Use QPalette to implement a palette class in Qt

QPalette sets the color of the control or window by obtaining the palette object ( palette ) of the control or window

The color ( ColorGroup ) of the different states of the widget can be set :

When QPalette::Active gets focus

When QPalette::inactive is not focused

In the state where QPalette::Disable is not available

Set the color of different parts ( ColorRole ):

QPalette::Window sets the background color of the form

QPalette::WindowText sets the text in the form (such as the text in Label, which cannot affect Edit

QPalette::Button sets the button (but it seems that it cannot be updated dynamically, and it has no effect when used directly

QPalette::ButtonText sets the color of the button text (QPushButton, small triangle of QComboBox

QPalette::Base is mainly used for the background color of the text input widget (the background color of the drop-down list of QComboBox will also change

function:

For setting a single color:

void QPalette::setColor(ColorGroup group, ColorRole role, const QColor &color)

void QPalette::setColor(ColorRole role, const QColor &color)

Colors for setting variety (image backgrounds, gradients, etc.:

void QPalette::setBrush(ColorGroup group, ColorRole role, const QBrush &brush)

void QPalette::setBrush(ColorRole role, const QBrush &brush)

An example from "Qt5 Development and Examples (Third Edition)"

Renderings:

 

Paste the code for color selection ComboBox

#include "combobox.h"
 
ComboBox::ComboBox(QWidget * parent)
 :QComboBox(parent)
{
 QStringList list = QColor::colorNames();//获取颜色列表
 for(QString str : list){
 QPixmap pix(QSize(70, 20)); //图像对象
 pix.fill(QColor(str)); //填充颜色
 addItem(QIcon(pix), str); //添加进ComboBox
 setIconSize(QSize(70,20)); //设置Icon也就是颜色块的大小
 
 //设置comboBox的下拉列表的尺寸调整策略为符合内容大小(自适应
 setSizeAdjustPolicy(QComboBox::AdjustToContents);
 }
}

Slot function that sets the color of each part

void Palette::setWindowColor(QString color)
{
 QPalette palette = this->palette();
 palette.setColor(QPalette::Window, QColor(color));
 this->setPalette(palette);
 
}
 
void Palette::setWindowTextColor(QString color)
{
 QPalette palette = showFrame->palette();
 palette.setColor(QPalette::WindowText, QColor(color));
 showFrame->setPalette(palette);
}
 
void Palette::setButtonColor(QString color)
{
 QPalette palette = showFrame->palette();
 palette.setColor(QPalette::Button, QColor(color));
 showFrame->setPalette(palette);
 
 showFrame->update();
}
 
void Palette::setBtnTextColor(QString color)
{
 QPalette palette = showFrame->palette();
 palette.setColor(QPalette::ButtonText, QColor(color));
 showFrame->setPalette(palette);
}
 
void Palette::setBaseColor(QString color)
{
 QPalette palette = showFrame->palette();
 palette.setColor(QPalette::Base, QColor(color));
 showFrame->setPalette(palette);
}

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_73443478/article/details/130689235