Qt Color Picker Widget?

2020-06-16 08:40发布

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?

4条回答
▲ chillily
2楼-- · 2020-06-16 09:11

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

查看更多
三岁会撩人
3楼-- · 2020-06-16 09:18

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").

查看更多
劳资没心,怎么记你
4楼-- · 2020-06-16 09:20

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.

查看更多
仙女界的扛把子
5楼-- · 2020-06-16 09:27

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();

public slots:
    void updateColor();
    void changeColor();

private:
    QColor color;
};

Implementation:

#include <QColorDialog>

SelectColorButton::SelectColorButton( QWidget* 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()
{
    return color;
}
查看更多
登录 后发表回答