QPalette类相当于对话框或控件的调色板,它管理着控件或窗体的所有颜色信息,每个窗体或控件都包含一个QPalette对象,在显示时按照它的QPalette对象中对各部分各状态下的颜色的描述来进行绘制。
QPalette类有两个基本的数据类型:一个是ColorGroup,用来区分不同状态下空间或窗体的颜色信息;另一个是ColorRole,用来区分控件或窗体的不同组成的颜色信息。其具体的解释如下:
ColorGroup:
QPalette::Disabled | 不可用状态 |
QPalette::Active | 活跃状态(获得焦点) |
QPalette::Inactive | 不活跃状态(未获得焦点) |
ColorRole:
QPalette::Window | 一个常规的背景颜色 |
QPalette::Background | 这个值是废弃的,使用window代替 |
QPalette::WindowText | 一个一般的前景颜色(文本颜色 ) |
QPalette::Foreground | 这个值是废弃的,使用windowText代替. |
QPalette::Base | 主要用作文本输入小部件(QTextEdit、QListView等)的背景颜色,也可以用于其他,如combobox下拉列表和toolbar。 |
QPalette::Text | 与QPalette::Base对应,主要用作文本输入小部件(QTextEdit、QListView等)的文本颜色 |
QPalette::AlternateBase | 被用来作视图行交替的背景颜色(当视图设置了行背景色交替属性setAlternatingRowColors(true)),与QPalette::Base进行区分 |
QPalette::ToolTipBase | 被用来作为背景颜色为QToolTip和QWhatsThis。工具尖端使用QPalette不活跃的颜色组,因为工具尖端不是活跃的窗口. |
QPalette::ToolTipText | 被用来作为前景颜色为QToolTip和QWhatsThis.工具尖端使用QPalette不活跃的颜色组,因为工具尖端不是活跃的窗口. |
QPalette::Button | button背景颜色。这个背景颜色能是不同于window作为一些风格,要求一个不同的背景颜色作为button |
QPalette::ButtonText | 一个前景颜色被用来作为button颜色. |
QPalette::BrightText | 一个text颜色是很不同于windowText,很好的对比与dark。典型的被用来为text,需要被画,在text或者windowText将给差的对比,就像在按下的button。注意text颜色能被用来为事情,而不只是单词;text颜色通常被用来为text,但是他是相当普通的使用text颜色角色为行,图标,等等。 |
QPalette主要通过以下四个函数来实现对控件或窗体的颜色设置:
void QPalette::setColor ( ColorRole role, const QColor & color );
void QPalette::setColor ( ColorGroup group, ColorRole role, const QColor & color );
void QPalette::setBrush ( ColorRole role, const QBrush & brush );
void QPalette::setBrush ( ColorGroup group, ColorRole role, const QBrush & brush );
其中,setBrush可以通过设置QBrush的图片来设置控件或窗体的背景图片。另外,需要注意的是在设置对话框和控件(QPushButton、QFrame等)的背景色时需要使用
void setAutoFillBackground ( bool enabled );设置其背景可填充。
接下来给出一个示例(Qt创建一个QWidget项目):
//Wdiget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QComboBox>
#include <QFrame>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
//成员变量
QFrame *sFrame;
QFrame *cFrame;
QComboBox *cbWin;
QComboBox *cbWinText;
QComboBox *cbBtn;
QComboBox *cbBtnText;
QComboBox *cbBase;
QComboBox *cbText;
QComboBox *cbAlter;
//成员函数
void seletFrame();
void contentFrame();
void fillComboBox(QComboBox *);
private slots:
void cbWin_s();
void cbWinText_s();
void cbBtn_s();
void cbBtnText_s();
void cbBase_s();
void cbText_s();
void cbAlter_s();
};
#endif // WIDGET_H
//Widget.cpp
#include "widget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QTextEdit>
#include <QStringList>
#include <QListView>
#include <QStringListModel>
#include <QLabel>
#include <QGroupBox>
#include <QPixmap>
#include <QIcon>
#include <QSizePolicy>
#include <QDebug>
#include <QLineEdit>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
seletFrame();
contentFrame();
QHBoxLayout *gHblayout = new QHBoxLayout(this);
gHblayout->addWidget(sFrame);
gHblayout->addWidget(cFrame);
gHblayout->setMargin(10);
gHblayout->setSpacing(5);
gHblayout->setSizeConstraint(QLayout::SetFixedSize);
}
Widget::~Widget()
{
}
void Widget::seletFrame()
{
sFrame = new QFrame;
QLabel *label1 = new QLabel("Qt::Window");
cbWin = new QComboBox;
fillComboBox(cbWin);
connect(cbWin,SIGNAL(activated(int)),this,SLOT(cbWin_s()));
QLabel *label2 = new QLabel("Qt::WindowText");
cbWinText = new QComboBox;
fillComboBox(cbWinText);
connect(cbWinText,SIGNAL(activated(int)),this,SLOT(cbWinText_s()));
//connect(cbWinText,&QComboBox::activated,this,&Widget::cbWinText_s());
QGridLayout *s1Layout = new QGridLayout;
s1Layout->addWidget(label1,0,0);
s1Layout->addWidget(cbWin,0,1);
s1Layout->addWidget(label2,1,0);
s1Layout->addWidget(cbWinText,1,1);
s1Layout->setSizeConstraint(QLayout::SetFixedSize);
QGroupBox *s1GroupBox = new QGroupBox("Window corlor");
s1GroupBox->setLayout(s1Layout);
QLabel *label3 = new QLabel("Qt::Button");
cbBtn = new QComboBox;
fillComboBox(cbBtn);
connect(cbBtn,SIGNAL(activated(int)),this,SLOT(cbBtn_s()));
QLabel *label4 = new QLabel("Qt::ButtonText");
cbBtnText = new QComboBox;
fillComboBox(cbBtnText);
connect(cbBtnText,SIGNAL(activated(int)),this,SLOT(cbBtnText_s()));
QLabel *label5 = new QLabel("Qt::Base");
cbBase = new QComboBox;
fillComboBox(cbBase);
connect(cbBase,SIGNAL(activated(int)),this,SLOT(cbBase_s()));
QLabel *label6 = new QLabel("Qt::Text");
cbText = new QComboBox;
fillComboBox(cbText);
connect(cbText,SIGNAL(activated(int)),this,SLOT(cbText_s()));
QLabel *label7 = new QLabel("Qt::AlternateBase");
cbAlter = new QComboBox;
fillComboBox(cbAlter);
connect(cbAlter,SIGNAL(activated(int)),this,SLOT(cbAlter_s()));
QGridLayout *sgLayout =new QGridLayout(sFrame);
sgLayout->addWidget(s1GroupBox,0,0,1,2); //合并单元格,从(0,0)格开始,占有1行2列
sgLayout->addWidget(label3,1,0);
sgLayout->addWidget(cbBtn,1,1);
sgLayout->addWidget(label4,2,0);
sgLayout->addWidget(cbBtnText,2,1);
sgLayout->addWidget(label5,3,0);
sgLayout->addWidget(cbBase,3,1);
sgLayout->addWidget(label6,4,0);
sgLayout->addWidget(cbText,4,1);
sgLayout->addWidget(label7,5,0);
sgLayout->addWidget(cbAlter,5,1);
sgLayout->setMargin(5);
}
void Widget::contentFrame()
{
cFrame = new QFrame();
cFrame->setLineWidth(2);
cFrame->setFrameStyle(QFrame::Box|QFrame::Raised);
QLabel *labString = new QLabel("please input a string");
QLineEdit *edtString = new QLineEdit;
QHBoxLayout *stringWidget = new QHBoxLayout;
stringWidget->addWidget(labString);
stringWidget->addWidget(edtString);
stringWidget->setSpacing(5);
QListView *cListV = new QListView;
QStringList cstrL;
cstrL << "男儿立志岀乡关"<<"学不成名誓不还"<<"青山处处埋忠骨"<<"何须马革裹尸还";
QStringListModel *cListM = new QStringListModel(cstrL);
cListV->setModel(cListM);
cListV->setAlternatingRowColors(true);
QTextEdit *cTextE = new QTextEdit("葡萄美酒夜光杯");
cTextE->append("欲饮琵琶马上催");
cTextE->append("醉卧沙场君莫笑");
cTextE->append("古来征战几人回");
QPushButton *okBtn = new QPushButton("确认");
QPushButton *cancalBtn = new QPushButton("取消");
QHBoxLayout *cHLayout = new QHBoxLayout;
cHLayout->addStretch(1);
cHLayout->addWidget(okBtn);
cHLayout->addWidget(cancalBtn);
QVBoxLayout *cVLayout = new QVBoxLayout(cFrame);
cVLayout->addLayout(stringWidget);
cVLayout->addWidget(cListV);
cVLayout->addWidget(cTextE);
cVLayout->addLayout(cHLayout);
cVLayout->setMargin(15);
cVLayout->setSpacing(5);
okBtn->setAutoFillBackground(true);
cancalBtn->setAutoFillBackground(true);
cFrame->setAutoFillBackground(true);
}
void Widget::fillComboBox(QComboBox *c)
{
QStringList colorList = QColor::colorNames();
QString colorName;
foreach(colorName,colorList)
{
QPixmap s_color = QPixmap(70,20);
s_color.fill(QColor(colorName));
//QIcon s_color = QIcon(colorName);
c->addItem(QIcon(s_color),NULL);
c->setIconSize(QSize(70,20));
c->setSizeAdjustPolicy(QComboBox::AdjustToContents);
}
}
void Widget::cbWin_s()
{
qDebug()<<"进入cbWin_s()槽函数";
QStringList colors = QColor::colorNames();
QPalette p = cFrame->palette();
p.setColor(QPalette::Window,QColor(colors[cbWin->currentIndex()]));
cFrame->setPalette(p);
}
void Widget::cbWinText_s()
{
QStringList colors = QColor::colorNames();
QPalette p = cFrame->palette();
p.setColor(QPalette::WindowText,QColor(colors[cbWinText->currentIndex()]));
cFrame->setPalette(p);
}
void Widget::cbBtn_s()
{
QStringList colors = QColor::colorNames();
QPalette p = cFrame->palette();
p.setColor(QPalette::Button,QColor(colors[cbBtn->currentIndex()]));
cFrame->setPalette(p);
}
void Widget::cbBtnText_s()
{
QStringList colors = QColor::colorNames();
QPalette p = cFrame->palette();
p.setColor(QPalette::ButtonText,QColor(colors[cbBtnText->currentIndex()]));
cFrame->setPalette(p);
}
void Widget::cbBase_s()
{
QStringList colors = QColor::colorNames();
QPalette p = cFrame->palette();
p.setColor(QPalette::Base,QColor(colors[cbBase->currentIndex()]));
cFrame->setPalette(p);
}
void Widget::cbText_s()
{
QStringList colors = QColor::colorNames();
QPalette p = cFrame->palette();
p.setColor(QPalette::Text,QColor(colors[cbText->currentIndex()]));
cFrame->setPalette(p);
}
void Widget::cbAlter_s()
{
QStringList colors = QColor::colorNames();
QPalette p = cFrame->palette();
p.setColor(QPalette::AlternateBase,QColor(colors[cbAlter->currentIndex()]));
cFrame->setPalette(p);
}
结果图:
本文本质是对https://www.cnblogs.com/hanzhaoxin/archive/2012/11/18/2775918.html文章的转载
根据自己的学习理解,为方便自己查看,做了些许调整。