1. 程式人生 > >QPalette實例教程(QWidget自帶的顏色設置工具,對Window的各個部分都可設置顏色)

QPalette實例教程(QWidget自帶的顏色設置工具,對Window的各個部分都可設置顏色)

num tails 使用 amp img span font let com

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

QPalette實例教程(QWidget自帶的顏色設置工具,對Window的各個部分都可設置顏色)