QPalette调色板类

QT提供的调色板类QPalette专门用于管理部件外观显示,相当于部件或对话框的调色板,管理他们所有的颜色信息。每个部件都包含一个QPalette对象,在显示时,按照它的QPalette对象中对各部分各状态下的颜色的描述进行绘制。

dialog.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "dialog.h"

Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
createCtrlFrame();
createContentFrame();
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->addWidget(m_ctrlFrame);
mainLayout->addWidget(m_contentFrame);
}

Dialog::~Dialog()
{

}

void Dialog::createCtrlFrame()
{
m_ctrlFrame = new QFrame;
m_ctrlFrame->setFrameStyle(QFrame::Sunken | QFrame::Box);
m_windowLabel = new QLabel("QPalette::Window:");
m_windowComboBox = new QComboBox;
fillColorList(m_windowComboBox);
connect(m_windowComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindow(int)));//根据选择颜色给窗体设置颜色
m_windowTextLabel = new QLabel("QPalette::WindowText");
m_windowTextComboBox = new QComboBox;
fillColorList(m_windowTextComboBox);
//选中时发送信号
connect(m_windowTextComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindowText(int)));

m_buttonLabel = new QLabel("QPalette::Button");
m_buttonCombox = new QComboBox;
fillColorList(m_buttonCombox);
connect(m_buttonCombox,SIGNAL(activated(int)),this,SLOT(ShowButton(int)));

m_buttonTextLabel = new QLabel("QPalette::ButtonText");
m_buttonTextComboBox = new QComboBox;
fillColorList(m_buttonTextComboBox);
connect(m_buttonTextComboBox,SIGNAL(activated(int)),this,SLOT(ShowButtonText(int)));

m_baseLabel = new QLabel("QPalette::Base");
m_baseComboBox = new QComboBox;
fillColorList(m_baseComboBox);
connect(m_baseComboBox,SIGNAL(activated(int)),this,SLOT(ShowBase(int)));

//布局
QGridLayout *mainLayout = new QGridLayout(m_ctrlFrame);
mainLayout->setSpacing(20);
mainLayout->addWidget(m_windowLabel,0,0);
mainLayout->addWidget(m_windowComboBox,0,1);
mainLayout->addWidget(m_windowTextLabel,1,0);
mainLayout->addWidget(m_windowTextComboBox,1,1);
mainLayout->addWidget(m_buttonLabel,2,0);
mainLayout->addWidget(m_buttonCombox,2,1);
mainLayout->addWidget(m_buttonTextLabel,3,0);
mainLayout->addWidget(m_buttonTextComboBox,3,1);
mainLayout->addWidget(m_baseLabel,4,0);
mainLayout->addWidget(m_baseComboBox,4,1);
}

void Dialog::createContentFrame()
{
m_contentFrame = new QFrame;
m_contentFrame->setAutoFillBackground(true);

m_label1 = new QLabel("请输入一个值");
m_label2 = new QLabel("请输入字符串");
m_comboBox1 = new QComboBox;
m_lineEdit = new QLineEdit;
m_textEdit = new QTextEdit;

m_okBtn = new QPushButton(QString("确认"));
m_cancelBtn = new QPushButton(QString("取消"));
m_okBtn->setAutoFillBackground(true);

QGridLayout *topLayout = new QGridLayout;
topLayout->addWidget(m_label1,0,0);
topLayout->addWidget(m_comboBox1,0,1);
topLayout->addWidget(m_label2,1,0);
topLayout->addWidget(m_lineEdit,1,1);
topLayout->addWidget(m_textEdit,2,0,1,2);

QHBoxLayout *bottomLayout = new QHBoxLayout;
bottomLayout->addWidget(m_okBtn);
bottomLayout->addWidget(m_cancelBtn);

QVBoxLayout *mainLayout = new QVBoxLayout(m_contentFrame);
mainLayout->addLayout(topLayout);
mainLayout->addLayout(bottomLayout);
}

void Dialog::fillColorList(QComboBox *comboBox)
{
QStringList colorList = QColor::colorNames();//获取所有颜色
QString color;
foreach (color, colorList)
{
QPixmap pix(QSize(70,20));//下拉框尺寸
pix.fill(QColor(color));
comboBox->addItem(QIcon(pix),NULL);//下拉菜单添加选项
comboBox->setIconSize(QSize(70,20));
comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);//根据内容适应尺寸
}
}

void Dialog::ShowWindow(int index)
{
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[index]);//获取用户选择的颜色
QPalette p = m_contentFrame->palette();//获取调色板
p.setColor(QPalette::Window,color);
m_contentFrame->setPalette(p);//设置调色板
m_contentFrame->update();//刷新

}

void Dialog::ShowWindowText(int index)
{
//找到颜色,获取调色板,设置调色板对指定位置颜色进行修改,刷新。
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[index]);
QPalette p = m_contentFrame->palette();
p.setColor(QPalette::WindowText,color);
m_contentFrame->setPalette(p);
m_contentFrame->update();
}

void Dialog::ShowButton(int index)
{
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[index]);
QPalette p = m_contentFrame->palette();
p.setColor(QPalette::Button,color);
m_contentFrame->setPalette(p);
m_contentFrame->update();
}

void Dialog::ShowButtonText(int index)
{
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[index]);
QPalette p = m_contentFrame->palette();
p.setColor(QPalette::ButtonText,color);
m_contentFrame->setPalette(p);
m_contentFrame->update();
}

void Dialog::ShowBase(int index)
{
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[index]);
QPalette p = m_contentFrame->palette();
p.setColor(QPalette::Base,color);
m_contentFrame->setPalette(p);
m_contentFrame->update();
}

dialog.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include<QFrame>
#include<QLabel>
#include<QComboBox>
#include<QTextEdit>
#include<QGridLayout>
#include<QLineEdit>
#include<QPushButton>

class Dialog : public QDialog
{
Q_OBJECT

public:
Dialog(QWidget *parent = 0);
~Dialog();
void createCtrlFrame();
void createContentFrame();
void fillColorList(QComboBox* comboBox);//给组合框添加颜色列表
public slots:
void ShowWindow(int index);
void ShowWindowText(int index);
void ShowButton(int index);
void ShowButtonText(int index);
void ShowBase(int index);
private:
QFrame *m_ctrlFrame;
QLabel *m_windowLabel;
QComboBox*m_windowComboBox;
QLabel *m_windowTextLabel;
QComboBox *m_windowTextComboBox;
QLabel *m_buttonLabel;
QComboBox * m_buttonCombox;
QLabel* m_buttonTextLabel;
QComboBox* m_buttonTextComboBox;
QLabel* m_baseLabel;
QComboBox *m_baseComboBox;
QFrame* m_contentFrame;
QLabel* m_label1;
QLabel* m_label2;
QComboBox* m_comboBox1;
QLineEdit* m_lineEdit;
QTextEdit* m_textEdit;
QPushButton* m_okBtn;
QPushButton* m_cancelBtn;

};



#endif // DIALOG_H

上述代码中调色板是如何知道指定位置代表的是什么内容?

WindowText,Button……等已经在QT内声明好了。

1
2
3
4
5
6
7
8
9
10
enum ColorRole { WindowText, Button, Light, Midlight, Dark, Mid,
Text, BrightText, ButtonText, Base,Window, Shadow,
Highlight, HighlightedText,
Link, LinkVisited,
AlternateBase,
NoRole,
ToolTipBase, ToolTipText,
NColorRoles = ToolTipText + 1,
Foreground = WindowText, Background = Window
};

相比较起来样式表功能更加强大,如果要实现的颜色效果不是很复杂,可以使用调色板。