Qt Color Picker Widget?
Solution 1
Have you looked at the QtColorPicker, part of Qt Solutions?
QtColorPicker provides a small widget in the form of a QComboBox
with a customizable set of predefined colors for easy and fast access. Clicking the ...
button will open the QColorDialog
. It's licensed under LGPL so with dynamic linking and proper attribution it can be used in commercial software. Search for QtColorPicker and you'll find it. Here's a link to one site that hosts many of the Qt Solutions components:
https://github.com/pothosware/PothosFlow/tree/master/qtcolorpicker
Solution 2
There's a very easy way to implement that using a QPushButton
to display the current color and pickup one when it is clicked:
Definition:
#include <QPushButton>
#include <QColor>
class SelectColorButton : public QPushButton
{
Q_OBJECT
public:
SelectColorButton( QWidget* parent );
void setColor( const QColor& color );
const QColor& getColor() const;
public slots:
void updateColor();
void changeColor();
private:
QColor color;
};
Implementation:
#include <QColorDialog>
SelectColorButton::SelectColorButton( QWidget* parent )
: QPushButton(parent)
{
connect( this, SIGNAL(clicked()), this, SLOT(changeColor()) );
}
void SelectColorButton::updateColor()
{
setStyleSheet( "background-color: " + color.name() );
}
void SelectColorButton::changeColor()
{
QColor newColor = QColorDialog::getColor(color, parentWidget());
if ( newColor != color )
{
setColor( newColor );
}
}
void SelectColorButton::setColor( const QColor& color )
{
this->color = color;
updateColor();
}
const QColor& SelectColorButton::getColor() const
{
return color;
}
Solution 3
Qt doesn't offer anything simpler than QColorDialog natively, but there are several color picking widgets as part of wwWidgets, a user made set of widgets for Qt (note that this is "wwWidgets" with a "w" and not "wxWidgets" with an "x").
Solution 4
I think QColorDialog is best suited for your application. If you want to go for something simpler, it will come with reduced functionality. I'm not aware of any standard widget in Qt offering such an option but you can try out the following:
QCombobox with each entry corresponding to a different color. You can maybe even have the colors of the names in their actual color.
One or more slider bars to adjust the hue, saturation, val or R,G,B components.
QLineEdit fields for individual R,G,B components. You can also have a signal / slot mechanism wherein once the user changes a color, the color shown to the user gets changed accordingly.
You can have '+' and '-' signs to increase / decrease the above color component values.
I hope the above gives you some ideas.
Freedom_Ben
Updated on July 23, 2020Comments
-
Freedom_Ben almost 4 years
I have a
QDialog
subclass that presents some options to the user for their selecting. One of these options is a color. I have seen theQColorDialog
, and I need something much simpler, that is also a regular widget so I can add to my layout as part of my dialog. Does Qt offer anything like this or will I have to make my own? If the latter, what is the best strategy? -
Freedom_Ben over 10 yearsThanks for the tip! wwWidgets looks cool. Unfortunately it is GPLv2 so I can't use it at work. However it would definitely solve the problem if the GPL weren't an issue
-
Freedom_Ben over 10 yearsThese are good suggestions, thank you! I may implement my own widget with something similar
-
Freedom_Ben about 10 yearsThat's exactly what I was looking for! Thanks
-
tro over 9 yearsLink in answer is dead.
-
z3ntu over 7 yearsLink is dead again.
-
remi almost 5 yearsLink still dead
-
Alexis Wilke about 2 yearsThis is great. I would advice to change the
setColor(const QColor color)
withsetColor(const QColor newColor)
so that way you can avoid thethis->color
syntax. Although on my end I prefer to have fields that start withf_...
orm_...
(i.e. then you'd havem_color = color
instead ofthis->color = color
). Having an introducer is not conventional in Qt, but it helps me avoid many bugs.