我是靠谱客的博主 高兴抽屉,最近开发中收集的这篇文章主要介绍QPalette调色板,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

  • Qt提供的调色板类QPalette专门用于管理部件的外观显示,相当于部件或对话框的调色板,管理他们所有的颜色信息。每个部件都包含一个QPalette对象,在显示时,按照它的QPalette对象中对各部分各状态下的颜色的描述进行绘制。

实现通过选择调色板中的颜色,来改变部件的颜色
在这里插入图片描述

新建桌面应用程序testQPalette,类名Dialog,基类QDialog,窗口左侧为控制区,右侧为调色板效果区。

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QFrame>
#include <QLabel>
#include <QComboBox>
#include <QTextEdit>

class Dialog : public QDialog{
    Q_OBJECT
public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

    void createCtrlFrame();     //初始化组合框区域
    void createContentFrame();  //初始化按钮区域
   
    void fillColorList(QComboBox *comboBox); //组合框中添加颜色下拉项

private slots:
    void onShowWindow(int index);    //更新窗口颜色
    void onShowWindowText(int index);//更新窗口文字颜色
    void onShowButton(int index);    //更新按钮颜色
    void onShowButtonText(int index);//更新按钮文字颜色
    void onShowBase(int index);      //更新背景颜色
private:
    QFrame *m_ctrlFrame;             //左侧组合框区域
    QLabel *m_windowLabel;           //窗口标签
    QComboBox *m_windowComboBox;     //设置窗体颜色下拉组合框
    QLabel *m_windowTextLabel;       //窗体文本标签
    QComboBox *m_windowTextComboBox; //设置窗体颜色文本下拉组合框
    QLabel *m_buttonLabel;           //按钮标签
    QComboBox *m_buttonComboBox;     //设置按钮颜色下拉组合框
    QLabel *m_buttonTextLabel;       //按钮文本标签
    QComboBox *m_buttonTextComboBox; //设置按钮颜色文本下拉组合框
    QLabel *m_baseLabel;             //背景色文本标签
    QComboBox *m_baseComboBox;       //设置背景颜色下拉组合框

    QFrame *m_contentFrame;          //右侧效果显示区域
    QLabel *m_label1;                //输入标签1
    QLabel *m_label2;                //输入标签2
    QComboBox *m_labelComboBox;      //标签颜色下拉组合框
    QLineEdit *m_lineEidt;           //行编辑器
    QTextEdit *m_textEdit;           //文本编辑器
    QPushButton *m_okBtn;            //确定按钮
    QPushButton *m_cancelBtn;        //取消按钮
};
#endif // DIALOG_H

 
dialog.cpp

#include "dialog.h"

#include <QGridLayout>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>


#define COLOR_COMBOBOX_HEIGHT   20
#define COLOR_COMBOBOX_WIDTH    70

Dialog::Dialog(QWidget *parent) : QDialog(parent){
    setWindowTitle("QPalette");				
    setWindowFlags(Qt::WindowCloseButtonHint);

    createCtrlFrame();		//初始化左侧组合框区域
    createContentFrame();	//初始化右侧显示效果区域

    QHBoxLayout *myLayout = new QHBoxLayout(this);
    myLayout->addWidget(m_ctrlFrame);
    myLayout->addWidget(m_contentFrame);
}

Dialog::~Dialog(){}

void Dialog::createCtrlFrame(){
    m_ctrlFrame = new QFrame;
    m_ctrlFrame->setFrameStyle(QFrame::Sunken | QFrame::Box);   //设置边框下沉/箱子效果

    //WindowColor
    m_windowLabel = new QLabel("QPalette::Window:");
    m_windowComboBox = new QComboBox;
    fillColorList(m_windowComboBox);
    connect(m_windowComboBox, SIGNAL(activated(int)), this, SLOT(onShowWindow(int)));

    //WindowTextColor
    m_windowTextLabel = new QLabel("QPalette::WindowText:");
    m_windowTextComboBox = new QComboBox;
    fillColorList(m_windowTextComboBox);
    connect(m_windowTextComboBox, SIGNAL(activated(int)), this, SLOT(onShowWindowText(int)));

    //BUttonColor
    m_buttonLabel = new QLabel("QPalette::Button:");
    m_buttonComboBox = new QComboBox;
    fillColorList(m_buttonComboBox);
    connect(m_buttonComboBox, SIGNAL(activated(int)), this, SLOT(onShowButton(int)));

    //BUttonTextColor
    m_buttonTextLabel = new QLabel("QPalette::ButtonText:");
    m_buttonTextComboBox = new QComboBox;
    fillColorList(m_buttonTextComboBox);
    connect(m_buttonTextComboBox, SIGNAL(activated(int)), this, SLOT(onShowButtonText(int)));

    //baseColor
    m_baseLabel = new QLabel("QPalette::Base:");
    m_baseComboBox = new QComboBox;
    fillColorList(m_baseComboBox);
    connect(m_baseComboBox, SIGNAL(activated(int)), this, SLOT(onShowBase(int)));

    QGridLayout *mainLayout = new QGridLayout(m_ctrlFrame);
    mainLayout->setSpacing(20);
    mainLayout->addWidget(m_windowLabel, 0, 0);
    mainLayout->addWidget(m_windowComboBox, 0, 1);
    mainLayout->addWidget(m_windowTextLabel, 1, 0);
    mainLayout->addWidget(m_windowTextComboBox, 1, 1);
    mainLayout->addWidget(m_buttonLabel, 2, 0);
    mainLayout->addWidget(m_buttonComboBox, 2, 1);
    mainLayout->addWidget(m_buttonTextLabel, 3, 0);
    mainLayout->addWidget(m_buttonTextComboBox, 3, 1);
    mainLayout->addWidget(m_baseLabel, 4, 0);
    mainLayout->addWidget(m_baseComboBox, 4, 1);

}

void Dialog::createContentFrame(){
    m_contentFrame = new QFrame;
    m_contentFrame->setAutoFillBackground(true);    //自动填充背景

    m_label1 = new QLabel("请输入一个值");
    m_label2 = new QLabel("请输入字符串");
    m_labelComboBox = new QComboBox;
    m_lineEidt = new QLineEdit;
    m_textEdit = new QTextEdit;

    m_okBtn = new QPushButton("确定");
    m_cancelBtn= new QPushButton("取消");
    m_okBtn->setAutoFillBackground(true);

    QGridLayout *TopLayout = new QGridLayout;
    TopLayout->addWidget(m_label1, 0,0);
    TopLayout->addWidget(m_labelComboBox, 0,1);
    TopLayout->addWidget(m_label2, 1,0);
    TopLayout->addWidget(m_lineEidt, 1,1);
    TopLayout->addWidget(m_textEdit, 2,0, 1,2);

    QHBoxLayout *bottomLayout = new QHBoxLayout;
    bottomLayout->addWidget(m_okBtn);
    bottomLayout->addWidget(m_cancelBtn);

    QVBoxLayout *mainLayou = new QVBoxLayout(m_contentFrame);
    mainLayou->addLayout(TopLayout);
    mainLayou->addLayout(bottomLayout);
}

void Dialog::fillColorList(QComboBox *comboBox){
    QStringList colorList = QColor::colorNames();  					//获取所有颜色
    QString color;
    foreach(color, colorList){
        QPixmap pix(QSize(COLOR_COMBOBOX_WIDTH, COLOR_COMBOBOX_HEIGHT));
        pix.fill(QColor(color));            						//填充颜色
        comboBox->addItem(QIcon(pix), NULL);						//下拉组合框中添加项颜色图标
        comboBox->setIconSize(pix.size());  						//设置下拉框中项的尺寸
        comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents); //根据图片内容适应尺寸策略   
    }
}

void Dialog::onShowWindow(int index){
    QStringList colorList = QColor::colorNames();   //获取颜色链表
    QColor color = QColor(colorList.at(index));     //获取选择的颜色值
    QPalette palette = m_contentFrame->palette();   //获取调色板
    palette.setColor(QPalette::Window, color);      //设置窗体颜色
    m_contentFrame->setPalette(palette);            //设置画家
    m_contentFrame->update();                       //刷新窗体重绘
}

void Dialog::onShowWindowText(int index){
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList.at(index));
    QPalette palette = m_contentFrame->palette();
    palette.setColor(QPalette::WindowText, color);  //设置窗体文字颜色
    m_contentFrame->setPalette(palette);
    m_contentFrame->update();
}

void Dialog::onShowButton(int index){
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList.at(index));
    QPalette palette = m_contentFrame->palette();
    palette.setColor(QPalette::Button, color);      //设置按钮颜色
    m_contentFrame->setPalette(palette);
    m_contentFrame->update();
}

void Dialog::onShowButtonText(int index){
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList.at(index));
    QPalette palette = m_contentFrame->palette();
    palette.setColor(QPalette::ButtonText, color);  //设置按钮文字颜色
    m_contentFrame->setPalette(palette);
    m_contentFrame->update();
}

void Dialog::onShowBase(int index){
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList.at(index));
    QPalette palette = m_contentFrame->palette();
    palette.setColor(QPalette::Base, color);        //设置背景颜色
    m_contentFrame->setPalette(palette);
    m_contentFrame->update();
}

最后

以上就是高兴抽屉为你收集整理的QPalette调色板的全部内容,希望文章能够帮你解决QPalette调色板所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部