Set a stylesheet for QColorDialog
-
Hi, so I just now realised, that I've made a mistake in my code example. Everytime you click cancel the color that it returns is black.
EDIT: It's a problem because I'm setting a widgets color, and when I click cancel, it sets without me wanting it to.
Here it's fixed:QString color; QColorDialog pick; pick.setStyleSheet("stylesheet"); pick.setCurrentColor("10, 255, 10"); //optional if (pick.exec() == QColorDialog::Accepted) { QColor const color = pick.selectedColor().name(); color = color.name(); qDebug() << color; }
Another EDIT: Here's an example of 2 different stylesheets:
Light mode:QColorDialog {background-color: rgb(244, 244, 244);} QPushButton {background-color: rgb(244, 244, 244); color: black;} QLabel {background-color: rgb(244, 244, 244); color: black;} QLineEdit {background-color: white; color: black;} QSpinBox {background-color: white; color: black;}
Dark mode:
QColorDialog {background-color: rgb(6, 6, 14);} QPushButton {color: rgb(211, 213, 201); background-color: rgb(36, 36, 44);} QLabel {color: rgb(211, 213, 201); background-color: rgb(6, 6, 14);} QLineEdit {color: rgb(211, 213, 201); background-color: rgb(36, 36, 44);} QSpinBox {color: rgb(211, 213, 201); background-color: rgb(36, 36, 44);}
-
Since QColorDialog::getColor() is a static function you won't be able to set a stylesheet when you want to use getColor(). Use a non-static function instead.
-
Since QColorDialog::getColor() is a static function you won't be able to set a stylesheet when you want to use getColor(). Use a non-static function instead.
@Christian-Ehrlicher, ok that works, but how do I get the selected color now?
-
@Christian-Ehrlicher, ok that works, but how do I get the selected color now?
-
Hi @JoeCFD, that didn't work for me, but I know I'm doing it wrong, but I don't know how to make it right. It says that I'm "missing emit keyword on signal call". Am I supposed to QObject::conect or something? If yes, please provide an example, because I don't know how to properly use that.
-
Hi @JoeCFD, that didn't work for me, but I know I'm doing it wrong, but I don't know how to make it right. It says that I'm "missing emit keyword on signal call". Am I supposed to QObject::conect or something? If yes, please provide an example, because I don't know how to properly use that.
-
@Sucharek
QColor QColorDialog::selectedColor() constReturns the color that the user selected by clicking the OK or equivalent button.
Hi @JonB, thanks, it works perfectly.
If anyone wants to use it, here's a snippet for it:QColorDialog pick; pick.setStyleSheet("stylesheet"); pick.setCurrentColor("10, 255, 10"); //optional pick.exec(); QColor const color = pick.selectedColor().name(); if (!color.isValid()) return; QString selectedCol = color.name(); qDebug() << selectedCol;
-
Hi, so I just now realised, that I've made a mistake in my code example. Everytime you click cancel the color that it returns is black.
EDIT: It's a problem because I'm setting a widgets color, and when I click cancel, it sets without me wanting it to.
Here it's fixed:QString color; QColorDialog pick; pick.setStyleSheet("stylesheet"); pick.setCurrentColor("10, 255, 10"); //optional if (pick.exec() == QColorDialog::Accepted) { QColor const color = pick.selectedColor().name(); color = color.name(); qDebug() << color; }
Another EDIT: Here's an example of 2 different stylesheets:
Light mode:QColorDialog {background-color: rgb(244, 244, 244);} QPushButton {background-color: rgb(244, 244, 244); color: black;} QLabel {background-color: rgb(244, 244, 244); color: black;} QLineEdit {background-color: white; color: black;} QSpinBox {background-color: white; color: black;}
Dark mode:
QColorDialog {background-color: rgb(6, 6, 14);} QPushButton {color: rgb(211, 213, 201); background-color: rgb(36, 36, 44);} QLabel {color: rgb(211, 213, 201); background-color: rgb(6, 6, 14);} QLineEdit {color: rgb(211, 213, 201); background-color: rgb(36, 36, 44);} QSpinBox {color: rgb(211, 213, 201); background-color: rgb(36, 36, 44);}