QPalette( [ˈpælət] 调色板)类相当于对话框或控件的调色板,它管理着控件或窗体的所有颜色信息,每个窗体或控件都包含一个QPalette对象,在显示时按照它的QPalette对象中对各部分各状态下的颜色的描述来进行绘制。

QPalette类有两个基本的概念,一个是ColorGroup,另一个是ColorRole。

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

ColorGroup:

QPalette::Disabled 不可用状态
QPalette::Active 活跃状态(获得焦点)
QPalette::Inactive 不活跃状态(未获得焦点)

ColorRole:

QPalette::Window 一个常规的背景颜色
QPalette::Background 这个值是废弃的,使用window代替
QPalette::WindowText 一个一般的前景颜色
QPalette::Foreground 这个值是废弃的,使用windowText代替.
QPalette::Base 最长使用来作为text背景颜色为整个widget,但是也能被用来为其他的绘画,像combobox的上下清单的背景和工具栏句柄。它通常是白的或者其他亮的颜色.
QPalette::AlternateBase 被用来作为轮流的背景颜色,轮流的行颜色
QPalette::ToolTipBase 被用来作为背景颜色为QToolTip和QWhatsThis。工具尖端使用QPalette不活跃的颜色组,因为工具尖端不是活跃的窗口.
QPalette::ToolTipText 被用来作为前景颜色为QToolTip和QWhatsThis.工具尖端使用QPalette不活跃的颜色组,因为工具尖端不是活跃的窗口.
QPalette::Text 前景颜色使用base.这通常和windowText相同,它一定提供好的对比window和base
QPalette::Button button背景颜色。这个背景颜色能是不同于window作为一些风格,要求一个不同的背景颜色作为button
QPalette::ButtonText 一个前景颜色被用来作为button颜色.
QPalette::BrightText 一个text颜色是很不同于windowText,很好的对比与dark。典型的被用来为text,需要被画,在text或者windowText将给差的对比,就像在按下的button。注意text颜色能被用来为事情,而不只是单词;text颜色通常被用来为text,但是他是相当普通的使用text颜色角色为行,图标,等等。

另外,在设置对话框和控件的背景色时还会用到:

void setAutoFillBackground ( bool enabled );

.h文件#include <QtWidgets/QDialog>
#include<QComboBox>
#include<QFrame>
#include "ui_Palette.h"class Palette : public QDialog
{Q_OBJECTpublic:Palette(QWidget *parent = Q_NULLPTR);void createSelectFrame();void createContentFrame();void fillColorList(QComboBox *);
public slots:void sl_window();void sl_windowText();void sl_button();void sl_buttonText();void sl_base();
private:QFrame *selectFrame;    //颜色选择面板  QFrame *contentFrame;   //具体显示面板  QComboBox *cbbWindow;QComboBox *cbbWindowText;QComboBox *cbbButton;QComboBox *cbbButtonText;QComboBox * cbbBase;
};
.cpp文件
#include "Palette.h"
#include <QPalette>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QStringList>
#include <QColor>
#include <QPixmap>
#include <QSpinBox>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#pragma execution_character_set("utf-8")
Palette::Palette(QWidget *parent): QDialog(parent)
{createSelectFrame();createContentFrame();QHBoxLayout *mainLayout = new QHBoxLayout(this);mainLayout->addWidget(selectFrame);mainLayout->addWidget(contentFrame);mainLayout->setMargin(10);mainLayout->setSpacing(5);//固定窗口的大小,且根据内部控件所占用的位置自动调节大小mainLayout->setSizeConstraint(QLayout::SetFixedSize);}
void Palette::createSelectFrame()
{selectFrame = new QFrame;QLabel *labWindow = new QLabel(tr("QPalette::Window:"));QLabel *labWindowText = new QLabel(tr("QPalette::WindowText:"));QLabel *labButton = new QLabel(tr("QPalette::Button:"));QLabel *labButtonText = new QLabel(tr("QPalette::ButtonText:"));QLabel *labBase = new QLabel(tr("QPalette::Base:"));//组合框cbbWindow = new QComboBox;fillColorList(cbbWindow);connect(cbbWindow, SIGNAL(activated(int)), this, SLOT(sl_window()));cbbWindowText = new QComboBox;fillColorList(cbbWindowText);connect(cbbWindowText, SIGNAL(activated(int)), this, SLOT(sl_windowText()));cbbButton = new QComboBox;fillColorList(cbbButton);connect(cbbButton, SIGNAL(activated(int)), this, SLOT(sl_button()));cbbButtonText = new QComboBox;fillColorList(cbbButtonText);connect(cbbButtonText, SIGNAL(activated(int)), this, SLOT(sl_buttonText()));cbbBase = new QComboBox;fillColorList(cbbBase);connect(cbbBase, SIGNAL(activated(int)), this, SLOT(sl_base()));QGridLayout *ctrlLayout = new QGridLayout(selectFrame);ctrlLayout->addWidget(labWindow, 0, 0);ctrlLayout->addWidget(labWindowText, 1, 0);ctrlLayout->addWidget(labButton, 2, 0);ctrlLayout->addWidget(labButtonText, 3, 0);ctrlLayout->addWidget(labBase, 4, 0);ctrlLayout->addWidget(cbbWindow, 0, 1);ctrlLayout->addWidget(cbbWindowText, 1, 1);ctrlLayout->addWidget(cbbButton, 2, 1);ctrlLayout->addWidget(cbbButtonText, 3, 1);ctrlLayout->addWidget(cbbBase, 4, 1);ctrlLayout->setMargin(5);  //设置边距ctrlLayout->setSpacing(5); //间距}
void Palette::fillColorList(QComboBox *cbb)
{QStringList colorNameList = QColor::colorNames();QString colorName;foreach(colorName, colorNameList){QPixmap pix_color(70, 20);pix_color.fill(QColor(colorName));cbb->addItem(QIcon(pix_color), NULL);cbb->setIconSize(QSize(70, 20));cbb->setSizeAdjustPolicy(QComboBox::AdjustToContents);   //设置下拉列表的尺寸符合内容的大小  }
}
void Palette::createContentFrame()
{contentFrame = new QFrame;QLabel *labValue = new QLabel("Please select one of the values");QSpinBox *spbValue = new QSpinBox;  QHBoxLayout *valueLayout =new QHBoxLayout;valueLayout->addWidget(labValue);valueLayout->addWidget(spbValue);valueLayout->setSpacing(5);QLabel *labString = new QLabel("please input a string");QLineEdit *edtString = new QLineEdit;QHBoxLayout *stringWidget = new QHBoxLayout;stringWidget->addWidget(labString);stringWidget->addWidget(edtString);stringWidget->setSpacing(5);QTextEdit *edtHello = new QTextEdit("hello Qt");QPushButton *btnOk = new QPushButton("OK");QPushButton *btnCancel = new QPushButton("Cancel");QHBoxLayout *buttonLayout = new QHBoxLayout;buttonLayout->addStretch(1);  //添加一个填充buttonLayout->addWidget(btnOk);buttonLayout->addWidget(btnCancel);buttonLayout->setSpacing(5);QVBoxLayout *contentLayout = new QVBoxLayout(contentFrame);contentLayout->addLayout(valueLayout);contentLayout->addLayout(stringWidget);contentLayout->addWidget(edtHello);contentLayout->addLayout(buttonLayout);btnOk->setAutoFillBackground(true);btnCancel->setAutoFillBackground(true);contentFrame->setAutoFillBackground(true);
}void Palette::sl_window()
{QStringList colorList = QColor::colorNames();QColor color = QColor(colorList[cbbWindow->currentIndex()]);//获取当前调色板QPalette p = contentFrame->palette();p.setColor(QPalette::Window,color);contentFrame->setPalette(p);
}
void Palette::sl_windowText()
{QStringList colorList = QColor::colorNames();QColor color = QColor(colorList[cbbWindowText->currentIndex()]);//获取当前调色板QPalette p = contentFrame->palette();p.setColor(QPalette::WindowText, color);contentFrame->setPalette(p);selectFrame->setPalette(p);}
void Palette::sl_button()
{QStringList colorList = QColor::colorNames();QColor color = QColor(colorList[cbbButton->currentIndex()]);//获取当前调色板QPalette p = contentFrame->palette();p.setColor(QPalette::Button, color);contentFrame->setPalette(p);}
void Palette::sl_buttonText()
{QStringList colorList = QColor::colorNames();QColor color = QColor(colorList[cbbButtonText->currentIndex()]);//获取当前调色板QPalette p = contentFrame->palette();p.setColor(QPalette::ButtonText, color);contentFrame->setPalette(p);}
void Palette::sl_base()
{QStringList colorList = QColor::colorNames();QColor color = QColor(colorList[cbbBase->currentIndex()]);//获取当前调色板QPalette p = contentFrame->palette();p.setColor(QPalette::Base, color);contentFrame->setPalette(p);
}

本文转自:http://hanzhaoxin.cnblogs.com/      作者:韩兆新

向大佬学习

QPalette类详细使用方法相关推荐

  1. java file_java开发之File类详细使用方法介绍

    File类简介 在 Java 中,File 类是 java.io 包中唯一代表磁盘文件本身的对象.File 类定义了一些与平台无关的方法来操作文件,File类主要用来获取或处理与磁盘文件相关的信息,像 ...

  2. C++类模板 template class T详细使用方法

    C++类模板 template <class T>详细使用方法 类模板与函数模板的定义和使用类似. 有时,有两个或多个类,其功能是相同的,仅仅是数据类型不同,如下面语句声明了一个类: cl ...

  3. 定义银行账户类Account,有属性:卡号cid,余额balance,所属用户Customer 银行账户类Account有方法: (1)getInfo(),返回String类型,返回卡的详细信息

    //定义银行账户类Account,有属性:卡号cid,余额balance,所属用户Customer //银行账户类Account有方法: //(1)getInfo(),返回String类型,返回卡的详 ...

  4. idea设置类注释和方法注释(贼详细)

    为了规范代码开发,我们需要用到开发工具的设置,比如类注释和方法注释的模板. 先说说在idea中如何设置类注释吧,第一步需要打开设置: 第二步打开文件和代码模板设置,并且找到类模板: /*** @Des ...

  5. IDEA设置类注释和方法注释的详细操作步骤

    文章目录 [前言] [正文] 类注释 方法注释 测试 [前言] 平时写代码的时候除了要写单行注释之外,还会写类注释和方法注释,类似下图的效果: 下面介绍一下如何在IDEA中设置类注释和方法注释: [正 ...

  6. pythongui界面实例带注释_python GUI库图形界面开发之PyQt5状态栏控件QStatusBar详细使用方法实例...

    PyQt5状态栏控件QStatusBar简介 MainWindow对象在底部保留有一个水平条,作为状态栏(QstatusBar),用于显示永久或临时的状态信息 QStatusBar类中的常用方法 方法 ...

  7. python表格控件_python GUI库图形界面开发之PyQt5表格控件QTableView详细使用方法与实例...

    PyQt5表格控件QTableView简介 在通常情况下,一个应用需要和一批数据进行交互,然后以表格的形式输出这些信息,这时就需要用到QTableView类了,在QTableView中可以使用自定义的 ...

  8. python日历gui_python GUI库图形界面开发之PyQt5日期时间控件QDateTimeEdit详细使用方法与实例...

    PyQt5日期时间控件QDateTimeEdit介绍 QDateTimeEdit是一个允许用户编辑日期时间的控件,可以使用键盘上的上下键头按钮来增加或减少日期的时间值,QDateTimeEdit通过s ...

  9. 1.2 内置异常类,异常方法

    Java 语言定义了一些异常类在 java.lang 标准包中. 标准运行时异常类的子类是最常见的异常类.由于 java.lang 包是默认加载到所有的 Java 程序的,所以大部分从运行时异常类继承 ...

  10. [转]NUnit详细使用方法

    本文转自:http://www.cnblogs.com/confach/archive/2005/06/20/177817.html 原文如下: ---------- NUnit2.0详细使用方法 注 ...

最新文章

  1. uart协议_UART协议简介
  2. OpenCV中的立体图像创建深度图
  3. 如何在android中水平放置三张图片,Android ACTION_VIEW多张图片
  4. Serverless 技术选型
  5. 应用的生命周期各个程序运行状态时代理的回调
  6. (单元测试)JavaBeans的技巧
  7. Linux mysql.plugin_Linux下MySQL安装
  8. koa 接口返回数据_koa+node基础搭建到实现api接口
  9. 网络排障少走弯路两案例速解网管疑难
  10. [转]OPENGL中GLU和GLUT工具箱
  11. 认认真真推荐10个牛逼公号!
  12. wps中打印图片去除黑边
  13. word怎么删除最后一页空白页
  14. Excel文件编辑保护如何取消?
  15. 空头平仓什么意思_空头开仓和空头平仓是什么意思(贵金属交易口诀)
  16. 如何删除“我的电脑”、“此电脑”中坚果云图标--三种方法(2020年的可行方案)
  17. django管理后台列表页,TextField字段展示换行等格式(format_html)
  18. 呀!原来这就是前端监控系统
  19. 阿里支付宝api对银行卡进行效验
  20. uni-app 图片上传实战

热门文章

  1. Excel数组与数组公式
  2. VIVADO中使用BD时,常用的IP
  3. 数据库原理及安全技术教学实验报告SQL实践(一)
  4. Centos备份文件
  5. OTA升级的三种方式
  6. Ansys Lumerical | 行波 Mach-Zehnder 调制器仿真分析
  7. Mysql事务操作及存储引擎
  8. 8.2 一些代数知识(群、循环群和子群)
  9. react 函数组件使用了 hook 后闪屏问题的分析与解决
  10. PETA 数据集下载