Qt Color Picker Widget?

21,825

Solution 1

Have you looked at the QtColorPicker, part of Qt Solutions?

QtColorPicker

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:

  1. QCombobox with each entry corresponding to a different color. You can maybe even have the colors of the names in their actual color.

  2. One or more slider bars to adjust the hue, saturation, val or R,G,B components.

  3. 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.

  4. You can have '+' and '-' signs to increase / decrease the above color component values.

I hope the above gives you some ideas.

Share:
21,825
Freedom_Ben
Author by

Freedom_Ben

Updated on July 23, 2020

Comments

  • Freedom_Ben
    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 the QColorDialog, 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
    Freedom_Ben over 10 years
    Thanks 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
    Freedom_Ben over 10 years
    These are good suggestions, thank you! I may implement my own widget with something similar
  • Freedom_Ben
    Freedom_Ben about 10 years
    That's exactly what I was looking for! Thanks
  • tro
    tro over 9 years
    Link in answer is dead.
  • z3ntu
    z3ntu over 7 years
    Link is dead again.
  • remi
    remi almost 5 years
    Link still dead
  • Alexis Wilke
    Alexis Wilke about 2 years
    This is great. I would advice to change the setColor(const QColor color) with setColor(const QColor newColor) so that way you can avoid the this->color syntax. Although on my end I prefer to have fields that start with f_... or m_... (i.e. then you'd have m_color = color instead of this->color = color). Having an introducer is not conventional in Qt, but it helps me avoid many bugs.