Qt入门-控件颜色面板类QPalette

来源:互联网 发布:淘宝怎么看海淘 编辑:程序博客网 时间:2024/04/30 03:38
QPalette类提供了绘制QWidget控件时使用的颜色。

控件的颜色状态分三种:

(1)Active,激活状态

(2)Disabled,禁用状态

(3)Inactive,未激活状态

控件在这三种不同的状态下具有不同的颜色值,QPalette类管理这三组颜色,它根据这三种状态分为三组颜色,每一组颜色都根据绘图角色的不同分类。系统在绘制控件时使用这些颜色,程序员可以改变这些设置。

设置的方法是先调用QWidget::palette()获取当前面板,修改它为自定义的值后再通过方法QWidget::setPalette设置为新修改的面板。代码如下所示:

[cpp] view plaincopyprint?
  1. QPalette palette = widget->palette();
  2. palette.setColor(QPalette::Window, Qt::lightGray); //改变控件背景色
  3. palette.setColor(QPalette::WindowText, Qt::blue); //改变控件字体颜色
  4. ...
  5. widget->setPalette(palette);


通过这种方法,可以方便设置控件的背景色,字体颜色等。

常用的设置方法如下:

(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的颜色值。

ColorGroup的值定义:

[cpp] view plaincopyprint?
  1. enum QPalette::ColorGroup
  2. Constant Value Description
  3. QPalette::Disabled 1
  4. QPalette::Active 0
  5. QPalette::Inactive 2
  6. QPalette::Normal Active synonym for Active


ColorRole的值定义:

[cpp] view plaincopyprint?
  1. The central roles are:
  2. Constant Value Description
  3. QPalette::Window 10 A general background color.
  4. QPalette::Background Window This value is obsolete. Use Window instead.
  5. QPalette::WindowText 0 A general foreground color.
  6. QPalette::Foreground WindowText This value is obsolete. Use WindowText instead.
  7. QPalette::Base 9 Used mostly as the background color for text entry widgets, but can also be usedfor other painting - such as the background of combobox drop down lists and toolbar handles. It is usually white or another light color.
  8. QPalette::AlternateBase 16 Used as the alternate background color in views with alternating row colors (see QAbstractItemView::setAlternatingRowColors()).
  9. QPalette::ToolTipBase 18 Used as the background color for QToolTip and QWhatsThis. Tool tips use the Inactive color group of QPalette, because tool tips are not active windows.
  10. QPalette::ToolTipText 19 Used as the foreground color for QToolTip and QWhatsThis. Tool tips use the Inactive color group of QPalette, because tool tips are not active windows.
  11. QPalette::Text 6 The foreground color used with Base. This is usually the same as the WindowText, in whichcase it must provide good contrast with Window and Base.
  12. QPalette::Button 1 The general button background color. This background can be different from Window as some styles require a different background colorfor buttons.
  13. QPalette::ButtonText 8 A foreground color used with the Button color.
  14. QPalette::BrightText 7 A text color that is very different from WindowText, and contrasts well with e.g. Dark. Typically usedfor text that needs to be drawn where Text or WindowText would give poor contrast, such as on pressed push buttons. Note that text colors can be usedfor things other than just words; text colors are usually usedfor text, but it's quite common to use the text color rolesfor lines, icons, etc.


原创粉丝点击