聊聊Qt中的Widget調色板QPalette


在實際的應用開發中,經常需要對某個窗體或某個控件的顏色外觀,如背景色、前景色等進行設置,已達到界面美化的效果。
Qt中的窗體或控件都是Widget類,Qt中提供的調色板QPalette類就是專門用於管理控件的外觀顯示。
QPalette類相當於對話框或控件的調色板,管理着控件和窗體的所有顏色。

QWidget中有個屬性palette(Qpalette)。
此屬性描述Widget的調色板。呈現標准組件時,該Widget的樣式使用該調色板,並且可以用作確保自定義Widget可以與本機平台的外觀保持一致的一種方式。通常,不同的平台或不同的樣式具有不同的調色板。
當您為窗口Widget分配新的調色板時,該調色板的顏色Role將與窗口Widget的默認調色板組合在一起,以形成窗口Widget的最終調色板。Widget的背景角色的調色板條目用於填充Widget的背景(請參閱QWidget :: autoFillBackground),而前景角色則初始化QPainter的筆。
默認值取決於系統環境。 QApplication維護一個系統/主題面板,它是所有Widget的默認選項。對於某些類型的Widget,可能還會有特殊的調色板默認設置(例如,在Windows Vista上,從QMenuBar派生的所有類都具有特殊的默認調色板)。您還可以通過將自定義調色板和Widget名稱傳遞給QApplication::setPalette()來自己為Widget定義默認調色板。最后,樣式始終可以選擇在分配調色板時對其進行拋光(請參見QStyle::polish())。
QWidget將顯式將調色板角色從父級傳播到子級。如果將畫筆或顏色分配給調色板上的特定角色,然后將該調色板分配給Widget,則該角色將傳播到所有Widget的子級,從而覆蓋該角色的所有系統默認值。請注意,除非啟用了Qt::WA_WindowPropagation屬性,否則默認情況下,調色板不會傳播到窗口(請參見isWindow())。
QWidget的調色板傳播類似於其字體傳播。
當前樣式用於呈現所有標准QtWidget的內容,可以自由地從Widget調色板中選擇顏色和畫筆,或者在某些情況下可以忽略(部分或全部)調色板。特別是,某些樣式(例如GTK樣式,Mac樣式和Windows Vista樣式)依賴於第三方API來呈現窗口Widget的內容,並且這些樣式通常不遵循調色板。因此,不能保證將角色分配給窗口Widget的調色板會更改窗口Widget的外觀。相反,您可以選擇應用styleSheet。
警告:請勿將此功能與Qt樣式表一起使用。使用樣式表時,可以使用“顏色”,“背景顏色”,“選擇顏色”,“選擇背景顏色”和“替代背景顏色”來自定義窗口小部件的調色板。
涉及的訪問函數:
const QPalette &palette() const 
void setPalette(const QPalette &)

QPalette有兩個基本的概念:一個是ColorGroup;一個是ColorRole。
ColorGroup有三種不同的狀態:

Active:激活狀態(獲得焦點狀態)
Disabled:禁用狀態(未獲得焦點狀態)
Inactive:未激活狀態(不可用狀態)
通常情況下:在大多數風格,活躍的和不活躍的看起來一樣。
ColorRole枚舉定義了當前GUI中使用的不同符號顏色角色。

核心Role包括:

 以下是一些主要用於3D斜角和陰影效果的顏色Role:

 高亮Role:

 超鏈接Role:

請注意,在Qt中渲染富文本時,我們不使用Link和LinkVisited角色,建議您使用CSS和QTextDocument::setDefaultStyleSheet()函數來更改鏈接的外觀。例如:

常用的設置顏色方法如下:
(1) void QPalette::setBrush ( ColorRole role, const QBrush & brush )
改變所有組下指定角色role的畫刷顏色值。
(2) void QPalette::setBrush ( ColorGroup group, ColorRole role, const QBrush & brush )
改變指定組group下的指定角色role的畫刷顏色值。
(3) void QPalette::setColor ( ColorRole role, const QColor & color )
改變所有組下指定角色role的顏色值。
(4) void QPalette::setColor ( ColorGroup group, ColorRole role, const QColor & color )
改變指定組group下指定角色role的顏色值。
注意:在以上代碼之前,必須先調用函數 setAutoFillBackground(true),設置窗體運行自動填充。
注意:在設置控件背景色填充時,一定要先調用setAutoFillBackground(true)函數,來運行自動填充背景。不然,程序中填充背景的代碼不會起作用的。

總之,通過調色板已經可以進行一定程度的窗體和控件美化。
實戰:應用換膚
新建基於Widget的應用


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>310</x>
     <y>270</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>PushButton</string>
   </property>
  </widget>
  <widget class="QToolButton" name="toolButton">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>37</width>
     <height>18</height>
    </rect>
   </property>
   <property name="text">
    <string>...</string>
   </property>
  </widget>
  <widget class="QCheckBox" name="checkBox">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>40</y>
     <width>71</width>
     <height>16</height>
    </rect>
   </property>
   <property name="text">
    <string>CheckBox</string>
   </property>
  </widget>
  <widget class="QRadioButton" name="radioButton">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>70</y>
     <width>89</width>
     <height>16</height>
    </rect>
   </property>
   <property name="text">
    <string>RadioButton</string>
   </property>
  </widget>
  <widget class="QCommandLinkButton" name="commandLinkButton">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>90</y>
     <width>172</width>
     <height>41</height>
    </rect>
   </property>
   <property name="text">
    <string>CommandLinkButton</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>130</y>
     <width>113</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
  <widget class="QTextEdit" name="textEdit">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>160</y>
     <width>161</width>
     <height>71</height>
    </rect>
   </property>
  </widget>
  <widget class="QDateTimeEdit" name="dateTimeEdit">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>240</y>
     <width>194</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;

    bool        defaultSkin;
    QPalette    defaultPal;
    QPalette    darkPal;
};

#endif // WIDGET_H


#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget),
    defaultSkin(true)
{
    ui->setupUi(this);
    defaultPal = qApp->palette();
    darkPal.setColor(QPalette::Window, QColor(53,53,53));
    darkPal.setColor(QPalette::WindowText, Qt::white);
    darkPal.setColor(QPalette::Base, QColor(15,15,15));
    darkPal.setColor(QPalette::AlternateBase, QColor(53,53,53));
    darkPal.setColor(QPalette::ToolTipBase, Qt::white);
    darkPal.setColor(QPalette::ToolTipText, Qt::white);
    darkPal.setColor(QPalette::Text, Qt::white);
    darkPal.setColor(QPalette::Button, QColor(53,53,53));
    darkPal.setColor(QPalette::ButtonText, Qt::white);
    darkPal.setColor(QPalette::BrightText, Qt::red);
    darkPal.setColor(QPalette::Highlight, QColor(142,45,197).lighter());
    darkPal.setColor(QPalette::HighlightedText, Qt::black);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    if (defaultSkin)
    {
        defaultSkin = false;
        qApp->setPalette(darkPal);
    }
    else {
        defaultSkin = true;
        qApp->setPalette(defaultPal);
    }
}
 1 #include "widget.h"
 2 #include <QApplication>
 3 #include <QStyleFactory>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     qApp->setStyle(QStyleFactory::create("Fusion"));
 9 
10     Widget w;
11     w.show();
12 
13     return a.exec();
14 }

構建運行:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM