现在的位置: 首页 > 综合 > 正文

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

2014年04月02日 ⁄ 综合 ⁄ 共 2613字 ⁄ 字号 评论关闭

    QPalette类提供了绘制QWidget控件时使用的颜色。

    控件的颜色状态分三种:

(1)Active,激活状态

(2)Disabled,禁用状态

(3)Inactive,未激活状态

 

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

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

QPalette palette = widget->palette();
palette.setColor(QPalette::Window, Qt::lightGray);  //改变控件背景色
palette.setColor(QPalette::WindowText, Qt::blue);   //改变控件字体颜色
...
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的值定义:

enum QPalette::ColorGroup

Constant	Value	Description
QPalette::Disabled	1	 
QPalette::Active	0	 
QPalette::Inactive	2	 
QPalette::Normal	Active	synonym for Active

ColorRole的值定义:

 

The central roles are:

Constant	Value	Description
QPalette::Window	10	A general background color.
QPalette::Background	Window	This value is obsolete. Use Window instead.
QPalette::WindowText	0	A general foreground color.
QPalette::Foreground	WindowText	This value is obsolete. Use WindowText instead.
QPalette::Base	9	Used mostly as the background color for text entry widgets, but can also be used for other painting - such as the background of combobox drop down lists and toolbar handles. It is usually white or another light color.
QPalette::AlternateBase	16	Used as the alternate background color in views with alternating row colors (see QAbstractItemView::setAlternatingRowColors()).
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.
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.
QPalette::Text	6	The foreground color used with Base. This is usually the same as the WindowText, in which case it must provide good contrast with Window and Base.
QPalette::Button	1	The general button background color. This background can be different from Window as some styles require a different background color for buttons.
QPalette::ButtonText	8	A foreground color used with the Button color.
QPalette::BrightText	7	A text color that is very different from WindowText, and contrasts well with e.g. Dark. Typically used for 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 used for things other than just words; text colors are usually used for text, but it's quite common to use the text color roles for lines, icons, etc.

 

抱歉!评论已关闭.