widgets/colorbutton.cpp

changeset 256
c6f5de03dc0f
parent 39
caac957e9834
child 257
afdab56e8210
equal deleted inserted replaced
255:5188f8a74a5c 256:c6f5de03dc0f
1 #include <QColorDialog>
2 #include "colorbutton.h"
3
4 ColorButton::ColorButton(const QColor& color, QWidget* parent) :
5 ColorButton{parent}
6 {
7 this->setColor(color);
8 }
9
10 ColorButton::ColorButton(QWidget* parent) :
11 QPushButton{parent}
12 {
13 connect(this, &QPushButton::clicked, [&](){
14 const QColor color = QColorDialog::getColor(this->storedSelectedColor, this->parentWidget());
15 if (color.isValid()) {
16 this->setColor(color);
17 }
18 });
19 this->setColor(Qt::black);
20 }
21
22 const QColor& ColorButton::color() const
23 {
24 return this->storedSelectedColor;
25 }
26
27 void ColorButton::setColor(const QColor& color)
28 {
29 if (this->storedSelectedColor != color) {
30 this->storedSelectedColor = color;
31 this->setStyleSheet(QString{"background-color: %1"}.arg(color.name()));
32 this->setText(color.name());
33 Q_EMIT this->colorChanged(this->storedSelectedColor);
34 }
35 }
36

mercurial