Qt dialog box design - use QPalette to change the color of the control

The QPalette class is equivalent to the palette of dialog boxes or controls. It manages all the color information of controls or forms. Each form or control contains a QPalette object, which is displayed according to its QPalette object. A description of the color in the state to draw.

The QPalette class has two basic concepts, one is ColorGroup and the other is ColorRole.

  • void QPalette::setColor ( ColorRolerole, const QColor &color );
  • void QPalette::setColor ( ColorGroupgroup, ColorRolerole, const QColor &color );
  • void QPalette::setBrush ( ColorRolerole, const QBrush &brush );
  • void QPalette::setBrush ( ColorGroupgroup, ColorRolerole, const QBrush &brush );

ColorGroup:

QPalette::Disabled

unavailable

QPalette::Active

active state (gain focus)

QPalette::Inactive

inactive state (not focused)

ColorRole:

QPalette::Window

a regular background color

QPalette::Background

This value is deprecated, use window instead

QPalette::WindowText

a general foreground color

QPalette::Foreground

This value is deprecated, use windowText instead.

QPalette::Base

Most commonly used as the text background color for the entire widget, but can also be used for other painting, like combobox backgrounds and toolbar handles. It is usually white or some other bright color.

QPalette::AlternateBase

Used as the background color of the rotation, the row color of the rotation

QPalette::ToolTipBase

Used as background color for QToolTip and QWhatsThis. The tooltip uses the QPalette's inactive color group because the tooltip is not the active window.

QPalette::ToolTipText

Used as the foreground color for QToolTip and QWhatsThis. The ToolTip uses the QPalette's inactive color set because the ToolTip is not the active window.

QPalette::Text

The foreground color uses base. This is usually the same as windowText, it must provide good contrast between window and base

QPalette::Button

button background color. This background color can be different from window as some styles require a different background color for button

QPalette::ButtonText

A foreground color is used as the button color.

QPalette::BrightText

A text color is very different from windowText, nice contrast with dark. Typically used for text that needs to be drawn, text or windowText will give poor contrast, like on pressed button s. Note that text colors can be used for things, not just words; text colors are usually used for text, but it's fairly common to use text color roles for lines, icons, etc.

In addition, it is also used when setting the background color of dialog boxes and controls:

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

void setAutoFillBackground ( bool enabled );

maindlg.h

    #ifndef MAINDLG_H  
    #define MAINDLG_H  
      
    #include <QtGui/QDialog>  
    #include <QFrame>  
    #include <QComboBox>  
      
      
    class MainDlg : public QDialog  
    {  
        Q_OBJECT  
      
    public:  
        MainDlg(QWidget *parent = 0, Qt::WFlags flags = 0);  
        ~MainDlg();  
      
        void createCtrlFrame();  
        void createContentFrame();  
        void fillColorList(QComboBox *);  
      
    public slots:  
        void sl_window();  
        void sl_windowText();  
        void sl_button();  
        void sl_buttonText();  
        void sl_base();  
      
    private:  
        QFrame *ctrlFrame;  //color selection panel
        QFrame *contentFrame;   //specific display panel
        QComboBox *cbbWindow;  
        QComboBox *cbbWindowText;  
        QComboBox *cbbButton;  
        QComboBox *cbbButtonText;  
        QComboBox * cbbBase;  
          
    };  
      
    #endif // MAINDLG_H  

maindlg.cpp

    #include "maindlg.h"  
    #include <QPalette>  
    #include <QHBoxLayout>  
    #include <QVBoxLayout>  
    #include <QGridLayout>  
    #include <QStringList>  
    #include <QColor>  
    #include <QPixmap>  
    #include <QSpinBox>  
    #include <QTextEdit>  
    #include <QLineEdit>  
    #include <QPushButton>  
    #include <QLabel>  
      
    MainDlg::MainDlg(QWidget *parent, Qt::WFlags flags)  
        : QDialog(parent, flags)  
    {  
        createCtrlFrame();  
        createContentFrame();  
      
        QHBoxLayout *mainLayout = new QHBoxLayout(this);  
        mainLayout->addWidget(ctrlFrame);  
        mainLayout->addWidget(contentFrame);  
        mainLayout->setMargin(10);  
        mainLayout->setSpacing(5);  
        mainLayout->setSizeConstraint(QLayout::SetFixedSize);  
    }  
      
    MainDlg::~MainDlg()  
    {  
      
    }  
      
    void MainDlg::fillColorList(QComboBox *cbb)  
    {  
        QStringList colorNameList = QColor::colorNames();  
      
        QString colorName;  
        foreach(colorName,colorNameList)  
        {  
            QPixmap pix_color(70,20);  
            pix_color.fill(QColor(colorName));  
      
            cbb->addItem(QIcon(pix_color),NULL);  
            cbb->setIconSize(QSize(70,20));  
            cbb->setSizeAdjustPolicy(QComboBox::AdjustToContents);   //Set the size of the drop-down list to match the size of the content
        }  
    }  
      
    void MainDlg::createCtrlFrame()  
    {  
        ctrlFrame = new QFrame;  
      
        QLabel *labWindow = new QLabel(tr("QPalette::Window:"));  
        QLabel *labWindowText = new QLabel(tr("QPalette::WindowText:"));  
        QLabel *labButton = new QLabel(tr("QPalette::Button:"));  
        QLabel *labButtonText = new QLabel(tr("QPalette::ButtonText:"));  
        QLabel *labBase = new QLabel(tr("QPalette::Base:"));  
      
        cbbWindow = new QComboBox;  
        fillColorList(cbbWindow);  
        connect(cbbWindow,SIGNAL(activated(int)),this,SLOT(sl_window()));  
        cbbWindowText = new QComboBox;  
        fillColorList(cbbWindowText);  
        connect(cbbWindowText,SIGNAL(activated(int)),this,SLOT(sl_windowText()));  
        cbbButton = new QComboBox;  
        fillColorList(cbbButton);  
        connect(cbbButton,SIGNAL(activated(int)),this,SLOT(sl_button()));  
        cbbButtonText = new QComboBox;  
        fillColorList(cbbButtonText);  
        connect(cbbButtonText,SIGNAL(activated(int)),this,SLOT(sl_buttonText()));  
        cbbBase = new QComboBox;  
        fillColorList(cbbBase);  
        connect(cbbBase,SIGNAL(activated(int)),this,SLOT(sl_base()));  
          
        int col_lab = 0;  
        int col_cbb = 1;  
        QGridLayout *ctrlLayout = new QGridLayout(ctrlFrame);  
        ctrlLayout->addWidget(labWindow,0,col_lab);  
        ctrlLayout->addWidget(labWindowText,1,col_lab);  
        ctrlLayout->addWidget(labButton,2,col_lab);  
        ctrlLayout->addWidget(labButtonText,3,col_lab);  
        ctrlLayout->addWidget(labBase,4,col_lab);  
        ctrlLayout->addWidget(cbbWindow,0,col_cbb);  
        ctrlLayout->addWidget(cbbWindowText,1,col_cbb);  
        ctrlLayout->addWidget(cbbButton,2,col_cbb);  
        ctrlLayout->addWidget(cbbButtonText,3,col_cbb);  
        ctrlLayout->addWidget(cbbBase,4,col_cbb);  
        ctrlLayout->setMargin(5);  
        ctrlLayout->setSpacing(5);  
    }  
      
    void MainDlg::createContentFrame()  
    {  
        contentFrame = new QFrame;  
          
        QLabel *labValue = new QLabel(tr("Please select one of the values:"));  
        QSpinBox *spbValue = new QSpinBox;  
        QHBoxLayout *valueLayout = new QHBoxLayout;  
        valueLayout->addWidget(labValue);  
        valueLayout->addWidget(spbValue);  
        valueLayout->setSpacing(5);  
      
        QLabel *labString = new QLabel(tr("Please input a string"));  
        QLineEdit *edtString = new QLineEdit;  
        QHBoxLayout *stringLayout = new QHBoxLayout;  
        stringLayout->addWidget(labString);  
        stringLayout->addWidget(edtString);  
        stringLayout->setSpacing(5);  
      
        QTextEdit *edtHelloQt = new QTextEdit(tr("Hello Qt!"));  
          
        QPushButton *btnOk = new QPushButton(tr("OK"));  
        QPushButton *btnCancel =new QPushButton(tr("Cancel"));  
        QHBoxLayout *buttonLayout = new QHBoxLayout;  
        buttonLayout->addStretch(1);  
        buttonLayout->addWidget(btnOk);  
        buttonLayout->addWidget(btnCancel);  
        buttonLayout->setSpacing(5);  
      
        QVBoxLayout *contentLayout = new QVBoxLayout(contentFrame);  
        contentLayout->addLayout(valueLayout);  
        contentLayout->addLayout(stringLayout);  
        contentLayout->addWidget(edtHelloQt);  
        contentLayout->addLayout(buttonLayout);  
        contentLayout->setMargin(5);  
        contentLayout->setSpacing(5);  
      
        btnOk->setAutoFillBackground(true);  
        btnCancel->setAutoFillBackground(true);  
        contentFrame->setAutoFillBackground(true);  
    }  
      
    void MainDlg::sl_window()  
    {  
        QStringList colorList = QColor::colorNames();  
        QColor color = QColor(colorList[cbbWindow->currentIndex()]);  
        QPalette p = contentFrame->palette();  
        p.setColor(QPalette::Window,color);  
        contentFrame->setPalette(p);  
    }  
      
    void MainDlg::sl_windowText()  
    {  
        QStringList colorList = QColor::colorNames();  
        QColor color = QColor(colorList[cbbWindowText->currentIndex()]);  
        QPalette p = contentFrame->palette();  
        p.setColor(QPalette::WindowText,color);  
        contentFrame->setPalette(p);  
    }  
      
    void MainDlg::sl_button()  
    {  
        QStringList colorNameList = QColor::colorNames();  
        QColor m_color = QColor(colorNameList[cbbButton->currentIndex()]);  
        QPalette p = contentFrame->palette();  
        p.setColor(QPalette::Button,m_color);  
        contentFrame->setPalette(p);  
    }  
      
    void MainDlg::sl_buttonText()  
    {  
        QStringList colorNameList = QColor::colorNames();  
        QColor m_color = QColor(colorNameList[cbbButtonText->currentIndex()]);  
        QPalette p = contentFrame->palette();  
        p.setColor(QPalette::ButtonText,m_color);  
        contentFrame->setPalette(p);  
    }  
      
      
    void MainDlg::sl_base()  
    {  
        QStringList colorNameList = QColor::colorNames();  
        QColor m_color = QColor(colorNameList[cbbBase->currentIndex()]);  
        QPalette p = contentFrame->palette();  
        p.setColor(QPalette::Base,m_color);  
        contentFrame->setPalette(p);  
    }  

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

Tags: Qt Qt6

Posted by kooza on Sun, 15 Jan 2023 18:17:30 +0530