QT基础之QPalette类


更多参见
QT基础与实例应用目录

代码链接

GitHub链接 :PaletteExample

QPalette简介

在QT中我们如何改变某个控件的颜色外观呢?比如改变背景、文字颜色等。QT提供了调色板类QPalette专门用于管理对话框的外观显示。每个控件或者窗体都包含一个QPalette对象,在显示时会根据其QPalette对象中对各个部分各状态下的颜色的描述进行绘制。

QPalette类有两个基本概念:一个是ColorGroup,另一个是ColorRole。详细可参见QT帮助手册。

ColorGroup指一下三种不同的状态:

  • QPalette::Active: 获得焦点的状态
  • QPalette::Inactive: 未获得焦点的状态
  • QPalette::Disabled:不可用状态

InactiveActive通常情况下颜色显示一致,也可根据情况设置。

请添加图片描述

ColorRole 用于对窗体不同部位颜色的分类,例如, QPalette::Window是指背景色,QPalette::WindowText指前景色
请添加图片描述

请添加图片描述
QPalette中几个重要的函数

//对不同ColorRole的不同ColorGroup设置颜色
void QPalette::setColor(QPalette::ColorGroup group, QPalette::ColorRole role, const QColor &color);
//对不同ColorRole设置颜色不区分ColorGroup
void QPalette::setColor(QPalette::ColorRole role, const QColor &color);
// 通过画刷的设置对显示进行修改,这样可能使用图片也不是单一的颜色对主题进行填充
void QPalette::setBrush(QPalette::ColorGroup group, QPalette::ColorRole role, const QBrush &brush);
void QPalette::setBrush(QPalette::ColorRole role, const QBrush &brush);

Demo以及说明

请添加图片描述

头文件

class Palette : public QDialog
{
   
    Q_OBJECT

public:
    Palette(QWidget *parent = nullptr);
    ~Palette();
private slots:
    void ShowWindow(); // 背景色
    void ShowWindowText(); // 前景色
    void ShowButton(); // 按钮背景色
    void ShowButtonText(); //按钮前景色
    void ShowBase();
private:
    void CreateCtrlFrame(); // 完成颜色选取区域的创建
    void CreateDisplayFrame(); // 完成展示区域的创建
    void fillColorList(QComboBox *comboBox); // 颜色选取区域 颜色下拉列表中插入颜色
private:
    QHBoxLayout *mainLayout;
    // 颜色选择部分
    QFrame *ctrlFrame;
    QLabel *windowLabel; // 背景色
    QComboBox *windowComboBox;
    QLabel *windowTextLabel; // 前景色
    QComboBox *windowTextComboBox;
    QLabel *buttonLabel; // 按钮背景色
    QComboBox *buttonComboBox;
    QLabel *buttonTextLabel; // 按钮前景色
    QComboBox *buttonTextComboBox;
    QLabel *baseLabel; // 主要用作文本输入小部件的背景颜色
    QComboBox *baseComboBox;

    //展示部分
    QFrame *displayFrame;
    QLabel *displayLabel1;
    QComboBox *displayComboBox;
    QLabel *displayLabel2;
    QLineEdit *displayLineEdit;
    QTextEdit *displayTextEdit;
    QPushButton *displayOkBtn;
    QPushButton *displayCancelBtn;
};

Cpp文件

Palette::Palette(QWidget *parent)
    
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值