Qt: 调色板QPalette类用法详解(附实例、源码)

  • 在实际的应用中,经常需要对某个控件的颜色外观,如背景、前景色等,进行设置。

  • Qt中提供的调色板QPalette类就是专门用于管理控件的外观显示。QPalette类相当于对话框或控件的调色板,管理着控件和窗体的所有颜色。

  • 每个窗体和控件都包含一个QPalette对象,在显示时,对其做相应的设置即可。

QPalette有两个基本的概念:一个是ColorGroup;一个是ColorRole。


ColorGroup有三种不同的状态:

  • Active:激活状态(获得焦点状态)

  • Disabled:禁用状态(未获得焦点状态)

  • Inactive:未激活状态(不可用状态)

通常情况下:在大多数风格,活跃的和不活跃的看起来一样。

原版英文描述如下:

The color groups:

The Active group is used for the window that has keyboard focus.
The Inactive group is used for other windows.
The Disabled group is used for widgets (not windows) that are disabled for some reason.
Both active and inactive windows can contain disabled widgets. (Disabled widgets are often called inaccessible or grayed out.)


ColorRole:


设置控件颜色的方法是先调用QWidget::palette()获取当前面板,修改它为自定义的值后再通过方法QWidget::setPalette设置为新修改的面板。代码如下所示

QPalette palette = widget->palette();  
palette.setColor(QPalette::Window, Qt::lightGray);  //改变控件背景色  
palette.setColor(QPalette::WindowText, Qt::blue);   //改变控件字体颜色  
...  
widget->setPalette(palette); 

常用的设置颜色方法如下:
(1) void QPalette::setBrush ( ColorRole role, const QBrush & brush )
改变所有组下指定角色role的画刷颜色值。

(2) void QPalette::setBrush ( ColorGroup group, ColorRole role, const QBrush & brush )
改变指定组group下的指定角色role的画刷颜色值。

(3) void QPalette::setColor ( ColorRole role, const QColor & color )
改变所有组下指定角色role的颜色值。

(4) void QPalette::setColor ( ColorGroup group, ColorRole role, const QColor & color )
改变指定组group下指定角色role的颜色值。


注意:在以上代码之前,必须先调用函数 setAutoFillBackground(true),设置窗体运行自动填充。


实例:

内容:利用QPalette改变控件颜色

效果如下:



步骤:

1、新建Qt GUI应用,项目名称为“Palette” (可以自定义,不过尽量见名知义),基类选择“QDialog”,类名为“Palette”,取消创建界面按钮。

一直单击下一步,完成创建工程。项目工程结构如下:



2、打开“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


3、打开“Palette.cpp”文件,添加如下代码:

#include "palette.h"
#include <QBoxLayout>
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;
    fillColorList(windowComboBox);
    connect(windowComboBox,SIGNAL(activated(int)),this,SLOT(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);

    okBtn->setAutoFillBackground(true);     //允许自动填充
    cancelBtn->setAutoFillBackground(true);
    contentFrame->setAutoFillBackground(true);
}

void Palette::showWindow()  //用于控制背景颜色的显示
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox->currentIndex()]);

   // contentFrame->setAutoFillBackground(true);

    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Window, color);
    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::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();
    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::showButton()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[buttonComboBox->currentIndex()]);

    //contentFrame->setAutoFillBackground(true);

    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Button , color);
    contentFrame->setPalette(p);

    contentFrame->update();

}

Palette::~Palette()
{

}


4、运行项目,效果如前所示。



注意:在设置控件背景色填充时,一定要先调用setAutoFillBackground(true)函数,来运行自动填充背景。不然,程序中填充背景的代码不会起作用的。

(陆文周先生的《Qt5开发及实例》这本教材中便忘记了添加这一句代码,所以程序中设置Window和Button的背景色这个功能没有效果,我就掉进了这个陷阱)。


英文文档读不懂怎么办,当然是多读书多记单词~\(≧▽≦)/~啦啦啦。

为什么要读书?

举个例子:
当你看到夕阳余晖…

你的脑海浮现的是:“落霞与孤鹜齐飞,秋水共长天一色。”

而不是:“卧槽,这么多鸟,真好看,真他妈太好看了!真屌,屌爆了!!” 
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页