1、基本介绍
在实际应用中,经常需要改变某个控件的颜色外观,如背景、文字颜色等。Qt提供的调色板类QPalette专门用于管理对话框的外观显示。QPalette类相当于对话框或控件的调色板,它管理着控件或窗体的所有颜色信息。每个窗体或控件都包含一个QPalette对象,在显示时,按照它的QPalette对象中对各部分各状态下的颜色描述进行绘制。
QPalette有两个基本的概念:ColorGroup、ColorRole
注:其中,Active状态与InActive状态在通常情况下,颜色显示是一致的,也可以根据需要设置为不一样的颜色。
QPalette类使用最多、最重要的函数是setColor()函数,其原型如下:
void QPalette::setColor(ColorGroup group,ColorRole role,const QColor & color);
在对主题颜色进行设置的同时,还区分了状态,即对某个主题在某个状态下的颜色进行了设置;
void QPalette::setColor(ColorRole role,const QColor & color);
只对某个主题的颜色进行设置,并不区分状态。
QPalette类同时还提供了setBrush()函数,通过画刷的设置对显示进行更改,这样就有可能使用图片而不是单一的颜色来对主题进行填充。
2、使用示例
QPalette p;
p.setColor(QPalette::Window,color);//p.setBrush(QPalette::Window,brush);
xxx->setPalette(p);
以下通过一个实例来介绍一下它的使用:
运行结果
3、详细代码
palette.h
#ifndef PALETTE_H
#define PALETTE_H
#include <QDialog>
#include <QComboBox>
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
#include <QLineEdit>
class Palette : public QDialog
{
Q_OBJECT
public:
Palette(QWidget *parent = 0);
~Palette();
void createCtrlFrame(); //完成窗体左半部分颜色选择区的创建
void createContentFrame(); //完成窗体右半部分的创建
void fillColorList(QComboBox *); //完成颜色下拉列表框中插入颜色的工作
private slots:
void showWindow();
void showWindowText();
void showButton();
void showButtonText();
void showBase();
private:
QFrame *CtrlFrame; //颜色选择面板
QLabel *windowLabel;
QComboBox *windowComboBox;
QLabel *windowTextLabel;
QComboBox *windowTextComboBox;
QLabel *buttonLabel;
QComboBox *buttonComboBox;
QLabel *buttonTextLabel;
QComboBox *buttonTextComboBox;
QLabel *baseLabel;
QComboBox *baseComboBox;
QFrame *contentFrame; //具体显示面板
QLabel *label1;
QComboBox *comboBox1;
QLabel *label2;
QLineEdit *lineEdit2;
QTextEdit *textEdit;
QPushButton *okBtn;
QPushButton *CancelBtn;
};
#endif // PALETTE_H
palette.cpp文章来源:https://www.toymoban.com/news/detail-735428.html
#include "palette.h"
#include <QHBoxLayout>
#include <QGridLayout>
Palette::Palette(QWidget *parent)
: QDialog(parent)
{
createCtrlFrame();
createContentFrame();
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->addWidget(CtrlFrame);
mainLayout->addWidget(contentFrame);
}
Palette::~Palette()
{
}
void Palette::createCtrlFrame()
{
CtrlFrame = new QFrame; //颜色选择面板
windowLabel = new QLabel(tr("QPalette::Window: "));
windowComboBox = new QComboBox;
fillColorList(windowComboBox); //向下拉列表框中插入各种不同的颜色选项
connect(windowComboBox,SIGNAL(activated(int)),this,SLOT(showWindow()));
windowTextLabel = new QLabel(tr("QPalete::WindowText: "));
windowTextComboBox = new QComboBox;
fillColorList(windowTextComboBox);
connect(windowTextComboBox,SIGNAL(activated(int)),this,SLOT(showWindowText()));
buttonLabel = new QLabel(tr("QPalette::Button: "));
buttonComboBox = new QComboBox;
fillColorList(buttonComboBox);
connect(buttonComboBox,SIGNAL(activated(int)),this,SLOT(showButton()));
buttonTextLabel = new QLabel(tr("QPalette::ButtonText: "));
buttonTextComboBox = new QComboBox;
fillColorList(buttonTextComboBox);
connect(buttonTextComboBox,SIGNAL(activated(int)),this,SLOT(showButtonText()));
baseLabel = new QLabel(tr("QPalette::Base: "));
baseComboBox = new QComboBox;
fillColorList(baseComboBox);
connect(baseComboBox,SIGNAL(activated(int)),this,SLOT(showBase()));
QGridLayout *mainLayout = new QGridLayout(CtrlFrame);
mainLayout->setSpacing(20);
mainLayout->addWidget(windowLabel,0,0);
mainLayout->addWidget(windowComboBox,0,1);
mainLayout->addWidget(windowTextLabel,1,0);
mainLayout->addWidget(windowTextComboBox,1,1);
mainLayout->addWidget(buttonLabel,2,0);
mainLayout->addWidget(buttonComboBox,2,1);
mainLayout->addWidget(buttonTextLabel,3,0);
mainLayout->addWidget(buttonTextComboBox,3,1);
mainLayout->addWidget(baseLabel,4,0);
mainLayout->addWidget(baseComboBox,4,1);
}
void Palette::createContentFrame()
{
contentFrame = new QFrame; //具体显示面板
label1 = new QLabel(tr("请选择一个值: "));
comboBox1 = new QComboBox;
label2 = new QLabel(tr("请输入字符串: "));
lineEdit2 = new QLineEdit;
textEdit = new QTextEdit;
QGridLayout *TopLayout = new QGridLayout;
TopLayout->addWidget(label1,0,0);
TopLayout->addWidget(comboBox1,0,1);
TopLayout->addWidget(label2,1,0);
TopLayout->addWidget(lineEdit2,1,1);
TopLayout->addWidget(textEdit,2,0,1,2);
okBtn = new QPushButton(tr("确认"));
CancelBtn = new QPushButton(tr("取消"));
QHBoxLayout *ButtomLayout = new QHBoxLayout;
ButtomLayout->addStretch(1);
ButtomLayout->addWidget(okBtn);
ButtomLayout->addWidget(CancelBtn);
QVBoxLayout *mainLayout = new QVBoxLayout(contentFrame);
mainLayout->addLayout(TopLayout);
mainLayout->addLayout(ButtomLayout);
okBtn->setAutoFillBackground(true); //允许自动填充
CancelBtn->setAutoFillBackground(true);
contentFrame->setAutoFillBackground(true);
}
void Palette::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 Palette::showWindow()
{
//获得当前选择的颜色值
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[windowComboBox->currentIndex()]);
QPalette p = contentFrame->palette();
p.setColor(QPalette::Window,color);
contentFrame->setPalette(p);
contentFrame->update();
}
void Palette::showWindowText()
{
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[windowComboBox->currentIndex()]);
QPalette p = contentFrame->palette();
p.setColor(QPalette::WindowText,color);
contentFrame->setPalette(p);
}
void Palette::showButton()
{
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[windowComboBox->currentIndex()]);
QPalette p = contentFrame->palette();
p.setColor(QPalette::Button,color);
contentFrame->setPalette(p);
contentFrame->update();
}
void Palette::showButtonText()
{
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[windowComboBox->currentIndex()]);
QPalette p = contentFrame->palette();
p.setColor(QPalette::ButtonText,color);
contentFrame->setPalette(p);
}
void Palette::showBase()
{
QStringList colorList = QColor::colorNames();
QColor color = QColor(colorList[windowComboBox->currentIndex()]);
QPalette p = contentFrame->palette();
p.setColor(QPalette::Base,color);
contentFrame->setPalette(p);
}
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓文章来源地址https://www.toymoban.com/news/detail-735428.html
到了这里,关于Qt之调色板(QPalette)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!