Qt5开发从入门到精通——第四篇九节(调色板)

欢迎小伙伴的点评✨✨,相互学习、互关必回、全天在线🍳🍳🍳
博主🧑🧑 本着开源的精神交流Qt开发的经验、将持续更新续章,为社区贡献博主自身的开源精神👩‍🚀


前言

本章节将会给大家带来调色板的详细使用方法


一、调色板概述

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

1.1、QPalette类详细介绍

在本节中详细介绍 QPalette 类的使用方法,该类有两个基本的概念:一个是 ColorGroup,另一个是 ColorRole。其中,ColorGroup 指的是以下三种不同的状态。

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

其中, Active 状态与 Inactive 状态在通常情况下,颜色显示是一致的,也可以根据需要设置为不一样的颜色。
ColorRole 指的是颜色主题,即对窗体中不同部位颜色的分类。例如, QPalette:: Window 是指背景色, QPalette::WindowText 是指前景色,等等。
QPalette 类使用最多、最重要的函数是 setColor() 函数,其原型如下:

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

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

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

只对某个主题的颜色进行设置,并不区分状态。
QPalette 类同时还提供了 setBrush() 函数,通过画刷的设置对显示进行更改,这样就有可能使用图片而不仅是单一的颜色来对主题进行填充。 Qt 之前的版本中有关背景色设置的函数如setBackgroundColor()或前景色设置的函数如 setForegroundColor()在 Qt5 中都被废止,统一由QPalette 类进行管理。例如, setBackgroundColor() 函数可由以下语句代替:

xxx->setAutoFillBackground(true);
QPalette p = xxx->palette ();
p. setColor (QPalette: :Window, color) ; //p. setBrush (QPalette·: :Window, brush) ;
xxx->setPalette(p);

二、效果实例

在这里插入图片描述

三、原码解析

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

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

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
    void createCtrlFrame();   //完成窗体左半部分颜色选择区的创建
    void createContentFrame() ; // 完成窗体右半部分的创建
    void fillColorList(QComboBox *); //完成向颜色下拉列表框中插入颜色的工作

private slots:
    void ShowWindow();
    void ShowWindowText();
    void ShowButton();
    void ShowButtonText();
    void ShowBase();

private:
    QFrame *ctrlFrame; //颜色选择板
    QLabel *windowLabe1;
    QComboBox *windowComboBox;
    QLabel *windowTextLabe1;
    QComboBox *windowTextComboBox;
    QLabel *buttonLabe1;
    QComboBox *buttonComboBox;
    QLabel *buttonTextLabel;
    QComboBox *buttonTextComboBox;
    QLabel *baseLabe1;
    QComboBox *baseComboBox;
    QFrame *contentFrame;    //具体显示面板
    QLabel *label1;
    QComboBox *comboBox1;
    QLabel *label2;
    QLineEdit *LineEdit2;
    QTextEdit *textEdit;
    QPushButton *OkBtn;
    QPushButton *CancelBtn;

};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include <QHBoxLayout>
#include <QGridLayout>
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    createCtrlFrame();
    createContentFrame();
    QHBoxLayout *mainLayout =new QHBoxLayout(this);
    mainLayout->addWidget(ctrlFrame);
    mainLayout->addWidget(contentFrame);
}

Dialog::~Dialog()
{

}
void Dialog::createCtrlFrame()
{
    ctrlFrame =new QFrame; //颜色选择面板
    windowLabe1 =new QLabel(tr("QPalette::Window: "));
    windowComboBox = new QComboBox;  //创建一个QComboBox对象
    fillColorList (windowComboBox);  //向下拉列表框中插入各种不同的颜色选项
    connect (windowComboBox, SIGNAL (activated(int)), this, SLOT (ShowWindow())); //连接下拉列表的activated()信号与改变背景色的槽函数
    windowTextLabe1 =new QLabel(tr("QPalette::WindowText: "));
    windowTextComboBox =new QComboBox;
    fillColorList(windowTextComboBox);
    connect(windowTextComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindowText()));
    buttonLabe1 =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()));
    baseLabe1 =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(windowLabe1,0,0);
    mainLayout->addWidget(windowComboBox,0,1);
    mainLayout->addWidget(windowTextLabe1,1,0);
    mainLayout->addWidget(windowTextComboBox,1,1);
    mainLayout->addWidget(buttonLabe1,2,0);
    mainLayout->addWidget(buttonComboBox,2,1);
    mainLayout->addWidget(buttonTextLabel,3,0);
    mainLayout->addWidget(buttonTextComboBox,3,1);
    mainLayout->addWidget(baseLabe1,4,0);
    mainLayout->addWidget(baseComboBox,4,1);

}

void Dialog::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 Dialog::ShowWindow()
{
    //获得当前选择的颜色值
    QStringList colorList = QColor::colorNames();
    QColor color= QColor(colorList[windowComboBox->currentIndex()]);
    QPalette p = contentFrame->palette();  //获得右部窗体contentFrame的调色板信息
    p.setColor(QPalette::Window, color); //设置 contentFrame 窗体的 Window 类颜色,即背景色,
                                         //setColor() 的第一个参数为设置的颜色主题,第二个参数为具体的颜色值。
    //把修改后的调色板信息应用到contentFrame窗体中,更新显示
    contentFrame->setPalette(p);
    contentFrame->update();

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

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

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

}

void Dialog::fillColorList(QComboBox *comboBox)
{
    QStringList colorList = QColor::colorNames();  //获得 Qt 所有内置名称的颜色名列表,返回的是一个字符串列表 colorList 。
    QString color;                                 //新建一个 QString 对象,为循环遍历做准备。
    foreach(color,colorList)                       //对颜色名列表进行遍历
    {
        QPixmap pix(QSize(70,20));                 //新建一个 QPixmap 对象 pix 作为显示颜色的图标。
        pix.fill(QColor(color));                   //为pix填充当前遍历的颜色
        comboBox->addItem(QIcon(pix), NULL);       //调用 QComboBox 的 additemO 函数为下拉列表框插入一个条目,
                                                   //并以准备好的 pix 作为插入条目的图标,名称设为 NULL, 即不显示颜色的名称。
        comboBox->setIconSize(QSize(70, 20));      //设置图标的尺寸,图标默认尺寸是一个方形,将它设置为与 pix 尺寸相同的长方形。
        comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents); //设置下拉列表框的尺寸调整策略为 AdjustToContents (符合内容的大小)。

    }
}

main.cpp

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

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

    return a.exec();
}


四、总结

调色板会在应用程序开发中经常用到的

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

东.'

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

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

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

打赏作者

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

抵扣说明:

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

余额充值