QPalette的详细使用示例

1. 功能简介:

QPalette是Qt中的调色板类,它提供的setColor()函数可改变控件的颜色,其原型为:

void QPalette::setColor(ColorRole acr, const QColor &acolor)

其中, ColorRole是个枚举,指的是颜色主题,QPalette::Window是指背景色,QPalette::WindowText指的是前景色, QPalette::Button指的是按钮的底色...

2. 实例

新建一个Qt Widgets Application工程, 基类为QDialog或QMainWindow,取消"创建界面"复选框

2.1 mainwindow.h 代码如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QComboBox>
#include <QTextEdit>
#include <QPushButton>
#include <QLineEdit>

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    
    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 // MAINWINDOW_H

2.2 mainwindow.cpp 代码如下:

#include "mainwindow.h"
#include <QHBoxLayout>
#include <QGridLayout>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    createCtrlFrame();
    createContentFrame();
    
    QWidget* mainWidget = new QWidget(this);
    this->setCentralWidget(mainWidget);
    
    QHBoxLayout* mainLayout = new QHBoxLayout(mainWidget);
    mainLayout->addWidget(ctrlFrame);
    mainLayout->addWidget(contentFrame);
}

MainWindow::~MainWindow()
{
}

void MainWindow::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 MainWindow::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->addWidget(OkBtn);
    BottomLayout->addWidget(CancelBtn);
    
    QVBoxLayout* mainLayout = new QVBoxLayout(contentFrame);
    mainLayout->addLayout(TopLayout);
    mainLayout->addLayout(BottomLayout);
}

void MainWindow::fillColorList(QComboBox *comboBox)
{
    //获取Qt所有知道名称的颜色名列表
    QStringList colorList = QColor::colorNames();
    QString color;
    foreach(color, colorList)
    {
        //QPixmap对象作为显示颜色的图标
        QPixmap pix(QSize(70, 20));
        pix.fill(QColor(color));
        comboBox->addItem(QIcon(pix), NULL);
        comboBox->setIconSize(QSize(70, 20));
        //设置下拉框的尺寸调整策略为符合内容的大小
        comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    }
}

void MainWindow::ShowWindow()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox->currentIndex()]);
    QPalette pal = contentFrame->palette();
    pal.setColor(QPalette::Window, color);
    contentFrame->setPalette(pal);
    contentFrame->update();
}

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

void MainWindow::ShowButton()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[buttonComboBox->currentIndex()]);
    QPalette pal = contentFrame->palette();
    pal.setColor(QPalette::Button, color);
    contentFrame->setPalette(pal);
    contentFrame->update();
}

void MainWindow::ShowButtonText()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[buttonTextComboBox->currentIndex()]);
    QPalette pal = contentFrame->palette();
    pal.setColor(QPalette::ButtonText, color);
    contentFrame->setPalette(pal);
}

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

2.3 运行界面如下:

注:本文参考了<Qt5 开发及实例(第三版)> (主编:陆文周)

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

宏笋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值