Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Color Picker Widget?

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?

like image 725
Freedom_Ben Avatar asked Aug 15 '13 16:08

Freedom_Ben


2 Answers

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

like image 72
Daniel Hedberg Avatar answered Oct 03 '22 01:10

Daniel Hedberg


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;
}
like image 26
jpo38 Avatar answered Oct 02 '22 23:10

jpo38