学习QT之调色板(QPalette)

本文介绍Qt中QPalette类的使用方法,包括ColorGroup和ColorRole概念,setColor()和setBrush()函数的应用,以及如何通过实例设置控件颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在实际应用中,经常需要改变某个控件的颜色外观,如背景、文字颜色等。Qt提供的调色板类QPalette专门用于管理对话框的外观显示。QPalette类相当于对话框或控件的调色板,它管理着控件或窗体的所有颜色信息。每个窗体或控件都包含一个QPalette对象,在显示时,按照它的QPalette对象中对各部分各状态下的颜色描述进行绘制。

QPalette有两个基本的概念:ColorGroup、ColorRole

ColorGroup说明
QPalette::Active获得焦点的状态
QPalette::InActive未获得焦点的状态
QPalette::Disable不可用状态

:其中,Active状态与InActive状态在通常情况下,颜色显示是一致的,也可以根据需要设置为不一样的颜色。

ColorRole说明
QPalette::Window窗体部件的背景色
QPalette::WindowText窗体部件的前景色
QPalette::Base文本输入窗口部件(比如QTextEdit,QLinedit,QPlainTextEdit等)的背景色
QPalette::Text与QPalette::Base一块使用,指文本输入窗口部件的前景色
QPalette::Button按钮窗口部件的背景色
QPalette::ButtonText指按钮窗口部件的前景色
QPalette::Highlight设置文字高亮时的背景颜色
QPalette::HighlightedText设置文字高亮时的颜色
QPalette::Link超链接文字颜色
QPalette::LinkVisted超链接文字访问后的颜色

QPalette类使用最多、最重要的函数是setColor()函数,其原型如下:

void QPalette::setColor(ColorGroup group,ColorRole role,const QColor & color);

在对主题颜色进行设置的同时,还区分了状态,即对某个主题在某个状态下的颜色进行了设置;

void QPalette::setColor(ColorRole role,const QColor & color);

只对某个主题的颜色进行设置,并不区分状态。

QPalette类同时还提供了setBrush()函数,通过画刷的设置对显示进行更改,这样就有可能使用图片而不是单一的颜色来对主题进行填充。

使用示例

QPalette p;
p.setColor(QPalette::Window,color);//p.setBrush(QPalette::Window,brush);
xxx->setPalette(p);

以下通过一个实例来介绍一下它的使用

一、运行结果

在这里插入图片描述

二、详细代码

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

#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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

贝勒里恩

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

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

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

打赏作者

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

抵扣说明:

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

余额充值