Fix appearance of ColorButton

Wed, 22 Jun 2022 23:32:34 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 22 Jun 2022 23:32:34 +0300
changeset 257
afdab56e8210
parent 256
c6f5de03dc0f
child 258
fe094d0687ad

Fix appearance of ColorButton

widgets/colorbutton.cpp file | annotate | diff | comparison | revisions
widgets/colorbutton.h file | annotate | diff | comparison | revisions
--- a/widgets/colorbutton.cpp	Wed Jun 22 22:59:56 2022 +0300
+++ b/widgets/colorbutton.cpp	Wed Jun 22 23:32:34 2022 +0300
@@ -1,4 +1,5 @@
 #include <QColorDialog>
+#include <QStyleFactory>
 #include "colorbutton.h"
 
 ColorButton::ColorButton(const QColor& color, QWidget* parent) :
@@ -28,9 +29,13 @@
 {
 	if (this->storedSelectedColor != color) {
 		this->storedSelectedColor = color;
-		this->setStyleSheet(QString{"background-color: %1"}.arg(color.name()));
 		this->setText(color.name());
+		QPalette palette{color};
+		const qreal lightness = color.lightnessF();
+		const QColor foreground = lightness < 0.4 ? Qt::white : Qt::black;
+		palette.setColor(QPalette::ButtonText, foreground);
+		this->setPalette(palette);
+		this->setStyle(QStyleFactory::create("Fusion"));
 		Q_EMIT this->colorChanged(this->storedSelectedColor);
 	}
 }
-
--- a/widgets/colorbutton.h	Wed Jun 22 22:59:56 2022 +0300
+++ b/widgets/colorbutton.h	Wed Jun 22 23:32:34 2022 +0300
@@ -8,12 +8,11 @@
 {
 	Q_OBJECT
 	Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
+	QColor storedSelectedColor;
 public:
 	ColorButton(const QColor& color = {}, QWidget* parent = nullptr);
 	ColorButton(QWidget* parent = nullptr);
 	const QColor& color() const;
 	Q_SLOT void setColor(const QColor& color);
 	Q_SIGNAL void colorChanged(const QColor& color);
-private:
-	QColor storedSelectedColor;
 };

mercurial