• 聊聊Qt中的Widget调色板QPalette


    在实际的应用开发中,经常需要对某个窗体或某个控件的颜色外观,如背景色、前景色等进行设置,已达到界面美化的效果。

    Qt中的窗体或控件都是Widget类,Qt中提供的调色板QPalette类就是专门用于管理控件的外观显示。

    QPalette类相当于对话框或控件的调色板,管理着控件和窗体的所有颜色。

    QWidget中有个属性palette(Qpalette)。

    此属性描述Widget的调色板。呈现标准组件时,该Widget的样式使用该调色板,并且可以用作确保自定义Widget可以与本机平台的外观保持一致的一种方式。通常,不同的平台或不同的样式具有不同的调色板。

    当您为窗口Widget分配新的调色板时,该调色板的颜色Role将与窗口Widget的默认调色板组合在一起,以形成窗口Widget的最终调色板。Widget的背景角色的调色板条目用于填充Widget的背景(请参阅QWidget :: autoFillBackground),而前景角色则初始化QPainter的笔。

    本文福利, 免费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT图像绘制,QT网络,QT数据库编程,QT项目实战,QT嵌入式开发,Quick模块等等)↓↓↓↓↓↓见下面↓↓文章底部点击免费领取↓↓

    默认值取决于系统环境。 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的应用

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>Widget</class>
    4. <widget class="QWidget" name="Widget">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>400</width>
    10. <height>300</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>Widget</string>
    15. </property>
    16. <widget class="QPushButton" name="pushButton">
    17. <property name="geometry">
    18. <rect>
    19. <x>310</x>
    20. <y>270</y>
    21. <width>75</width>
    22. <height>23</height>
    23. </rect>
    24. </property>
    25. <property name="text">
    26. <string>PushButton</string>
    27. </property>
    28. </widget>
    29. <widget class="QToolButton" name="toolButton">
    30. <property name="geometry">
    31. <rect>
    32. <x>10</x>
    33. <y>10</y>
    34. <width>37</width>
    35. <height>18</height>
    36. </rect>
    37. </property>
    38. <property name="text">
    39. <string>...</string>
    40. </property>
    41. </widget>
    42. <widget class="QCheckBox" name="checkBox">
    43. <property name="geometry">
    44. <rect>
    45. <x>10</x>
    46. <y>40</y>
    47. <width>71</width>
    48. <height>16</height>
    49. </rect>
    50. </property>
    51. <property name="text">
    52. <string>CheckBox</string>
    53. </property>
    54. </widget>
    55. <widget class="QRadioButton" name="radioButton">
    56. <property name="geometry">
    57. <rect>
    58. <x>10</x>
    59. <y>70</y>
    60. <width>89</width>
    61. <height>16</height>
    62. </rect>
    63. </property>
    64. <property name="text">
    65. <string>RadioButton</string>
    66. </property>
    67. </widget>
    68. <widget class="QCommandLinkButton" name="commandLinkButton">
    69. <property name="geometry">
    70. <rect>
    71. <x>10</x>
    72. <y>90</y>
    73. <width>172</width>
    74. <height>41</height>
    75. </rect>
    76. </property>
    77. <property name="text">
    78. <string>CommandLinkButton</string>
    79. </property>
    80. </widget>
    81. <widget class="QLineEdit" name="lineEdit">
    82. <property name="geometry">
    83. <rect>
    84. <x>10</x>
    85. <y>130</y>
    86. <width>113</width>
    87. <height>20</height>
    88. </rect>
    89. </property>
    90. </widget>
    91. <widget class="QTextEdit" name="textEdit">
    92. <property name="geometry">
    93. <rect>
    94. <x>10</x>
    95. <y>160</y>
    96. <width>161</width>
    97. <height>71</height>
    98. </rect>
    99. </property>
    100. </widget>
    101. <widget class="QDateTimeEdit" name="dateTimeEdit">
    102. <property name="geometry">
    103. <rect>
    104. <x>10</x>
    105. <y>240</y>
    106. <width>194</width>
    107. <height>22</height>
    108. </rect>
    109. </property>
    110. </widget>
    111. </widget>
    112. <layoutdefault spacing="6" margin="11"/>
    113. <resources/>
    114. <connections/>
    115. </ui>
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. namespace Ui {
    5. class Widget;
    6. }
    7. class Widget : public QWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit Widget(QWidget *parent = nullptr);
    12. ~Widget();
    13. private slots:
    14. void on_pushButton_clicked();
    15. private:
    16. Ui::Widget *ui;
    17. bool defaultSkin;
    18. QPalette defaultPal;
    19. QPalette darkPal;
    20. };
    21. #endif // WIDGET_H
    22. #include "widget.h"
    23. #include "ui_widget.h"
    24. Widget::Widget(QWidget *parent) :
    25. QWidget(parent),
    26. ui(new Ui::Widget),
    27. defaultSkin(true)
    28. {
    29. ui->setupUi(this);
    30. defaultPal = qApp->palette();
    31. darkPal.setColor(QPalette::Window, QColor(53,53,53));
    32. darkPal.setColor(QPalette::WindowText, Qt::white);
    33. darkPal.setColor(QPalette::Base, QColor(15,15,15));
    34. darkPal.setColor(QPalette::AlternateBase, QColor(53,53,53));
    35. darkPal.setColor(QPalette::ToolTipBase, Qt::white);
    36. darkPal.setColor(QPalette::ToolTipText, Qt::white);
    37. darkPal.setColor(QPalette::Text, Qt::white);
    38. darkPal.setColor(QPalette::Button, QColor(53,53,53));
    39. darkPal.setColor(QPalette::ButtonText, Qt::white);
    40. darkPal.setColor(QPalette::BrightText, Qt::red);
    41. darkPal.setColor(QPalette::Highlight, QColor(142,45,197).lighter());
    42. darkPal.setColor(QPalette::HighlightedText, Qt::black);
    43. }
    44. Widget::~Widget()
    45. {
    46. delete ui;
    47. }
    48. void Widget::on_pushButton_clicked()
    49. {
    50. if (defaultSkin)
    51. {
    52. defaultSkin = false;
    53. qApp->setPalette(darkPal);
    54. }
    55. else {
    56. defaultSkin = true;
    57. qApp->setPalette(defaultPal);
    58. }
    59. }
    1. #include "widget.h"
    2. #include
    3. #include
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. qApp->setStyle(QStyleFactory::create("Fusion"));
    8. Widget w;
    9. w.show();
    10. return a.exec();
    11. }

    构建运行:


     

     本文福利, 免费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT图像绘制,QT网络,QT数据库编程,QT项目实战,QT嵌入式开发,Quick模块等等)↓↓↓↓↓↓见下面↓↓文章底部点击免费领取↓↓

  • 相关阅读:
    最长异或路径 ---- (字典树求异或最大)
    使用STM32控制TMC5160驱动步进电机
    【大模型的一些基本结论】
    1.8 - 多级存储
    Array.from()的使用方法(数组去重,伪数组转为数组,数组浅克隆),Set和Map数据结构
    案例分享-https证书链不完整导致请求失败
    Python3用OpenCV4连接图像
    Nginx安装与常见命令
    操作系统的基本概念
    离散优化算法和连续优化算法
  • 原文地址:https://blog.csdn.net/m0_60259116/article/details/127690768