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

来源:互联网 发布:淘宝运营公司排名 编辑:程序博客网 时间:2024/05/16 06:52

    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::ColorGroupConstantValueDescriptionQPalette::Disabled1 QPalette::Active0 QPalette::Inactive2 QPalette::NormalActivesynonym for Active


ColorRole的值定义:

 

The central roles are:ConstantValueDescriptionQPalette::Window10A general background color.QPalette::BackgroundWindowThis value is obsolete. Use Window instead.QPalette::WindowText0A general foreground color.QPalette::ForegroundWindowTextThis value is obsolete. Use WindowText instead.QPalette::Base9Used 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::AlternateBase16Used as the alternate background color in views with alternating row colors (see QAbstractItemView::setAlternatingRowColors()).QPalette::ToolTipBase18Used 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::ToolTipText19Used 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::Text6The 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::Button1The general button background color. This background can be different from Window as some styles require a different background color for buttons.QPalette::ButtonText8A foreground color used with the Button color.QPalette::BrightText7A 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.


 

原创粉丝点击