How to use C++ Qt QColorDialog

Qt provides a color selection box, as shown below:

 

How to use QColorDialog

For example, the following code, click the button to pop up the color selection box, select the color, and change the background color of QLabel

#include "widget.h"
#include "ui_widget.h"
#include <QColorDialog>
#include <QPalette>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->label->setText(u8"我是lable");
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_btn1_clicked()
{
    QColor color = QColorDialog::getColor(Qt::white, this, u8"选择颜色");
    if(color.isValid())
    {
        QPalette palette;
        palette.setColor(QPalette::Background ,color);
        ui->label->setAutoFillBackground(true);
        ui->label->setPalette(palette);
    }
}

The effect is as follows:

 

getColor call

getColor is declared as follows:

static QColor getColor(const QColor &initial = Qt::white,
                           QWidget *parent = nullptr,
                           const QString &title = QString(),
                           ColorDialogOptions options = ColorDialogOptions());

The first parameter is the default color. For example, in the above code, I set it directly to white. The
second parameter is the parent window pointer.
The third is the color dialog window title.

QPalette setColor

The declaration of setColor is as follows:

inline void QPalette::setColor(ColorRole acr, const QColor &acolor)

You can use the color returned by QColorDialog to directly construct a QPalette. Note that the first parameter of setColor needs a ColorRole, which can be referred to as follows:

enum ColorRole { WindowText, Button, Light, Midlight, Dark, Mid,
                     Text, BrightText, ButtonText, Base, Window, Shadow,
                     Highlight, HighlightedText,
                     Link, LinkVisited,
                     AlternateBase,
                     NoRole,
                     ToolTipBase, ToolTipText,
                     PlaceholderText,
                     NColorRoles = PlaceholderText + 1,
#if QT_DEPRECATED_SINCE(5, 13)
                     Foreground Q_DECL_ENUMERATOR_DEPRECATED_X("Use QPalette::WindowText instead") = WindowText,
                     Background Q_DECL_ENUMERATOR_DEPRECATED_X("Use QPalette::Window instead") = Window
#endif
                   };

For example, the above code is to modify the background color of Label, then you need to use the Background enumerator, and if you change the text, use Text.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_60259116/article/details/130171729