概述
前言
Qt提供的调色板类 QPalette 专门用于管理部件的外观显示,相当于部件或对话框的调色板,管理他们所有的颜色信息,每一个部件都包含一个 QPalette 对象,在显示时,按照它的 QPalette 对象中对各部分各状态下的颜色的描述进行绘制调色板运用
效果图:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QFrame>
#include <QLabel>
#include <QComboBox>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
public:
void createCtrlFrame();
void createContentFrame();
void fillColorList(QComboBox* comboBox); //组合框添加颜色链表
private 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_buttonLabelComboBox;
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_canceBtn;
};
#endif // DIALOG_H
#include "dialog.h"
#include <QGridLayout>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
setWindowTitle("QPalette");
setWindowFlags(Qt::WindowCloseButtonHint);
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("QPatte::Button:");
m_buttonLabelComboBox = new QComboBox;
fillColorList(m_buttonLabelComboBox);
connect(m_buttonLabelComboBox,SIGNAL(activated(int)),this,SLOT(ShowButton(int)));
m_buttonTextLabel = new QLabel("QPatte::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_buttonLabelComboBox,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_canceBtn = 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_canceBtn);
//垂直布局
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();
}
最后
以上就是鲤鱼煎饼为你收集整理的Qt QPalette调色板前言的全部内容,希望文章能够帮你解决Qt QPalette调色板前言所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复