QPalette实例教程(QWidget自带的颜色设置工具,对Window的各个部分都可设置颜色)


QPalette是一款非常好用的颜色设置工具:

头文件:#include <QPalette> (^-^我没有用这个头文件也可以使用QPalette)

常用函数:

void setBrush(ColorRole role, const QBrush & brush) void setBrush(ColorGroup group, ColorRole role, const QBrush & brush) void setColor(ColorGroup group, ColorRole role, const QColor & color) void setColor(ColorRole role, const QColor & color)
  • 1
  • 2
  • 3
  • 4

ColorGroup属性:

enum QPalette::​ColorGroup
Constant Value Description QPalette::Disabled 1 QPalette::Active 0 QPalette::Inactive 2 QPalette::Normal Active synonym for Active
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

ColorRole属性:

QPalette::Window 背景颜色 QPalette::WindowText 文本颜色 QPalette::Background 同QPalette::Window QPalette::Foreground 同QPalette::WindowText QPalette::Base 主要用作文本输入小部件的背景颜色,也可以用于其他,如combobox下拉列表 和toolbar的背景多为白色或另一种浅色。 QPalette::AlternateBase 在交替行颜色的视图中作为交替背景色 QPalette::ToolTipBase QToolTip和QWhatsThis的背景色 QPalette::ToolTipText QToolTip和QWhatsThis的前景色 QPalette::Text foreground和Base一起使用,通常与WindowText类似 QPalette::Button 按钮背景色 QPalette::ButtonText 按钮文本颜色 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

简易Demo:

#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->pushButton->setAutoFillBackground(true);//设置自动填充背景色,如果不需要填充背景色,此行代码可省略 QPalette palette = ui->pushButton->palette();//建立调色板对象 palette.setColor(QPalette::ButtonText, Qt::blue);//设置按钮字体颜色 palette.setColor(QPalette::Button, Qt::red); //设置按钮背景色,需要设置setAutoFillBackground(true) ui->pushButton->setPalette(palette); //控件使用调色板 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

效果展示: 
使用QPalette之前: 
这里写图片描述

使用QPalette之后: 
这里写图片描述

https://blog.csdn.net/qq_40194498/article/details/79696236


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM