QT学习:调色板

在实际应用中,经常需要改变某个控件的颜色外观,如背景、文字颜色等。Qt提供的调色板类QPalette专门用于管理对话框的外观显示。QPalette类相当于对话框或控件的调色板,它管理着控件或窗体的所有颜色信息。每个窗体或控件都包含一个QPalette对象,在显示时,按照它的QPalette对象中对各部分各状态下的颜色的描述进行绘制。
在介绍具体用法时我们先来了解以下基本概念,QPalette类有两个基本的概念: 一个是 ColorGroup另一个是ColorRole。其中,ColorGroup 指的是以下三种不同的状态。
(1)QPalette::Active:获得焦点的状态。
(2)QPalette:Inactive:未获得焦点的状态。
(3)QPalette:Disable:不可用状态。
其中,Active 状态与Inactive状态在通常情况下,颜色显示是一致的, 也可以根据需要设置为不一样 的颜色。
ColorRole指的是颜色主题,即对窗体中不同部位颜色的分类。例如,Plte:Window是指背景色,QPalette::WindowText 是指前景色。
QPalette类使用最多、最重要的函数是setColor()函数,此外QPalette类同时还提供了setBrush()函数, 通过画刷的设置对显示进行更改,这样就有可能使用图片而不仅是单一的颜色来对主题进行填充。Qt之前的版本中有关背景色设置的函数如setBackgroundColor()在 Qt5中都被废止,统一由QPalette类进行管理。
接下来我们用代码来熟悉具体用法:
头文件为:

#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

cpp文件为:

#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);
}

void Palette::createCtrlFrame()
{
    ctrlFrame =new QFrame;                  //颜色选择面板
    windowLabel =new QLabel(tr("QPalette::Window: "));
    windowComboBox =new QComboBox;			//创建一个QComboBox对象
    fillColorList(windowComboBox);			//向下拉列表框中插入各种不同的颜色选项
    connect(windowComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindow()));//连接下拉列表框的active()信号与高边背景色的槽函数ShowWindow()
    windowTextLabel =new QLabel(tr("QPalette::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 *BottomLayout =new QHBoxLayout;
    BottomLayout->addStretch(1);
    BottomLayout->addWidget(OkBtn);
    BottomLayout->addWidget(CancelBtn);
    QVBoxLayout *mainLayout =new QVBoxLayout(contentFrame);
    mainLayout->addLayout(TopLayout);
    mainLayout->addLayout(BottomLayout);
}

void Palette::ShowWindow()
{
    //获得当前选择的颜色值
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox->currentIndex()]);
    QPalette p = contentFrame->palette();		//获得右部窗体contentFrame的调色板信息
    p.setColor(QPalette::Window,color);			//设置窗体的Window类颜色,即背景色
    //把修改后的调色板信息应用到contentFrame窗体中,更新显示
    contentFrame->setPalette(p);
    contentFrame->update();
}

void Palette::ShowWindowText()
{
    QStringList colorList = QColor::colorNames();
    QColor color = colorList[windowTextComboBox->currentIndex()];
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::WindowText,color);
    contentFrame->setPalette(p);
}

void Palette::ShowButton()
{
    QStringList colorList = QColor::colorNames();
    QColor color =QColor(colorList[buttonComboBox->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[buttonTextComboBox->currentIndex()]);
    QPalette p =contentFrame->palette();
    p.setColor(QPalette::ButtonText,color);
    contentFrame->setPalette(p);
}

void Palette::ShowBase()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[baseComboBox->currentIndex()]);
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Base,color);
    contentFrame->setPalette(p);
}

void Palette::fillColorList(QComboBox *comboBox)
{
    QStringList colorList = QColor::colorNames();	//获得qt所有内置名称的颜色名列表
    QString color;									
    foreach(color,colorList)						//对颜色名列表进行遍历
    {
        QPixmap pix(QSize(70,20));					//新建对象作为显示颜色的图标
        pix.fill(QColor(color));                    //为pix填充当前遍历的颜色
        comboBox->addItem(QIcon(pix),NULL);         //为下拉列表框插入条目
        comboBox->setIconSize(QSize(70,20));		//设置图标尺寸
        comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
                                                    //设置下拉列表的尺寸调整策略
    }
}

Palette::~Palette()
{

}

main函数内容为:

#include "palette.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Palette w;
    w.show();

    return a.exec();
}

运行结果如下图所示:
在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值