QPalette 类

The QPalette 类包含各 Widget 状态的颜色组。 更多...

头: #include <QPalette>
qmake: QT += gui

公共类型

enum ColorGroup { Disabled, Active, Inactive, Normal }
enum ColorRole { Window, Background, WindowText, Foreground, ..., NoRole }

公共函数

QPalette ()
QPalette (const QColor & button )
QPalette (Qt::GlobalColor button )
QPalette (const QColor & button , const QColor & window )
QPalette (const QBrush & windowText , const QBrush & button , const QBrush & light , const QBrush & dark , const QBrush & mid , const QBrush & text , const QBrush & bright_text , const QBrush & base , const QBrush & window )
QPalette (const QPalette & p )
QPalette (QPalette && other )
~QPalette ()
const QBrush & alternateBase () const
const QBrush & base () const
const QBrush & brightText () const
const QBrush & brush (QPalette::ColorGroup group , QPalette::ColorRole role ) const
const QBrush & brush (QPalette::ColorRole role ) const
const QBrush & button () const
const QBrush & buttonText () const
qint64 cacheKey () const
const QColor & color (QPalette::ColorGroup group , QPalette::ColorRole role ) const
const QColor & color (QPalette::ColorRole role ) const
QPalette::ColorGroup currentColorGroup () const
const QBrush & dark () const
const QBrush & highlight () const
const QBrush & highlightedText () const
bool isBrushSet (QPalette::ColorGroup cg , QPalette::ColorRole cr ) const
bool isCopyOf (const QPalette & p ) const
bool isEqual (QPalette::ColorGroup cg1 , QPalette::ColorGroup cg2 ) const
const QBrush & light () const
const QBrush & link () const
const QBrush & linkVisited () const
const QBrush & mid () const
const QBrush & midlight () const
const QBrush & placeholderText () const
QPalette resolve (const QPalette & other ) const
void setBrush (QPalette::ColorRole role , const QBrush & brush )
void setBrush (QPalette::ColorGroup group , QPalette::ColorRole role , const QBrush & brush )
void setColor (QPalette::ColorGroup group , QPalette::ColorRole role , const QColor & color )
void setColor (QPalette::ColorRole role , const QColor & color )
void setColorGroup (QPalette::ColorGroup cg , const QBrush & windowText , const QBrush & button , const QBrush & light , const QBrush & dark , const QBrush & mid , const QBrush & text , const QBrush & bright_text , const QBrush & base , const QBrush & window )
void setCurrentColorGroup (QPalette::ColorGroup cg )
const QBrush & shadow () const
void swap (QPalette & other )
const QBrush & text () const
const QBrush & toolTipBase () const
const QBrush & toolTipText () const
const QBrush & window () const
const QBrush & windowText () const
QVariant operator QVariant () const
bool operator!= (const QPalette & p ) const
QPalette & operator= (const QPalette & p )
QPalette & operator= (QPalette && other )
bool operator== (const QPalette & p ) const

静态公共成员

const QMetaObject staticMetaObject
QDataStream & operator<< (QDataStream & s , const QPalette & p )
QDataStream & operator>> (QDataStream & s , QPalette & p )

详细描述

The QPalette 类包含各 Widget 状态的颜色组。

调色板由 3 个颜色组组成: Active , Disabled ,和 Inactive . All widgets in Qt contain a palette and use their palette to draw themselves. This makes the user interface easily configurable and easier to keep consistent.

If you create a new widget we strongly recommend that you use the colors in the palette rather than hard-coding specific colors.

The color groups:

  • The Active group is used for the window that has keyboard focus.
  • The Inactive group is used for other windows.
  • The Disabled group is used for widgets (not windows) that are disabled for some reason.

Both active and inactive windows can contain disabled widgets. (Disabled widgets are often called inaccessible or grayed out )。

In most styles, Active and Inactive look the same.

Colors and brushes can be set for particular roles in any of a palette's color groups with setColor () 和 setBrush (). A color group contains a group of colors used by widgets for drawing themselves. We recommend that widgets use color group roles from the palette such as "foreground" and "base" rather than literal colors like "red" or "turquoise". The color roles are enumerated and defined in the ColorRole 文档编制。

We strongly recommend that you use the default palette of the current style (returned by QGuiApplication::palette ()) and modify that as necessary. This is done by Qt's widgets when they are drawn.

To modify a color group you call the functions setColor () 和 setBrush (), depending on whether you want a pure color or a pixmap pattern.

There are also corresponding color () 和 brush () getters, and a commonly used convenience function to get the ColorRole for the current ColorGroup : window (), windowText (), base (),等。

You can copy a palette using the copy constructor and test to see if two palettes are identical 使用 isCopyOf ().

QPalette is optimized by the use of 隐式共享 , so it is very efficient to pass QPalette objects as arguments.

警告: Some styles do not use the palette for all drawing, for instance, if they make use of native theme engines. This is the case for both the Windows Vista and the macOS styles.

另请参阅 QApplication::setPalette (), QWidget::setPalette (),和 QColor .

成员类型文档编制

enum QPalette:: ColorGroup

常量 描述
QPalette::Disabled 1
QPalette::Active 0
QPalette::Inactive 2
QPalette::Normal Active synonym for Active

enum QPalette:: ColorRole

Color Roles

The ColorRole enum defines the different symbolic color roles used in current GUIs.

The central roles are:

常量 描述
QPalette::Window 10 一般背景色。
QPalette::Background Window This value is obsolete. Use Window instead.
QPalette::WindowText 0 一般前景色。
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::PlaceholderText 20 Used as the placeholder color for various text input widgets. This enum value has been introduced in Qt 5.12
QPalette::Text 6 The foreground color used with . This is usually the same as the WindowText , in which case it must provide good contrast with Window and .
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 颜色。
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 文本 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.

There are some color roles used mostly for 3D bevel and shadow effects. All of these are normally derived from Window , and used in ways that depend on that relationship. For example, buttons depend on it to make the bevels look attractive, and Motif scroll bars depend on Mid to be slightly different from Window .

常量 描述
QPalette::Light 2 Lighter than Button 颜色。
QPalette::Midlight 3 Between Button and Light .
QPalette::Dark 4 Darker than Button .
QPalette::Mid 5 Between Button and Dark .
QPalette::Shadow 11 A very dark color. By default, the shadow color is Qt::black .

Selected (marked) items have two roles:

常量 描述
QPalette::Highlight 12 A color to indicate a selected item or the current item. By default, the highlight color is Qt::darkBlue .
QPalette::HighlightedText 13 A text color that contrasts with Highlight . By default, the highlighted text color is Qt::white .

There are two color roles related to hyperlinks:

常量 描述
QPalette::Link 14 A text color used for unvisited hyperlinks. By default, the link color is Qt::blue .
QPalette::LinkVisited 15 A text color used for already visited hyperlinks. By default, the linkvisited color is Qt::magenta .

Note that we do not use the Link and LinkVisited roles when rendering rich text in Qt, and that we recommend that you use CSS and the QTextDocument::setDefaultStyleSheet () function to alter the appearance of links. For example:

    QTextBrowser browser;
    QColor linkColor(Qt::red);
    QString sheet = QString::fromLatin1("a { text-decoration: underline; color: %1 }").arg(linkColor.name());
    browser.document()->setDefaultStyleSheet(sheet);
					
常量 描述
QPalette::NoRole 17 No role; this special role is often used to indicate that a role has not been assigned.

成员函数文档编制

QPalette:: QPalette ()

Constructs a palette object that uses the application's default palette.

另请参阅 QApplication::setPalette () 和 QApplication::palette ().

QPalette:: QPalette (const QColor & button )

Constructs a palette from the button color. The other colors are automatically calculated, based on this color. Window will be the button color as well.

QPalette:: QPalette ( Qt::GlobalColor button )

Constructs a palette from the button color. The other colors are automatically calculated, based on this color. Window will be the button color as well.

QPalette:: QPalette (const QColor & button , const QColor & window )

Constructs a palette from a button color and a window . The other colors are automatically calculated, based on these colors.

QPalette:: QPalette (const QBrush & windowText , const QBrush & button , const QBrush & light , const QBrush & dark , const QBrush & mid , const QBrush & text , const QBrush & bright_text , const QBrush & base , const QBrush & window )

Constructs a palette. You can pass either brushes, pixmaps or plain colors for windowText , button , light , dark , mid , text , bright_text , base and window .

另请参阅 QBrush .

QPalette:: QPalette (const QPalette & p )

构造副本为 p .

此构造函数很快感谢 隐式共享 .

QPalette:: QPalette ( QPalette && other )

移动构造 QPalette 实例,使之指向同一对象如 other 所指向的。

After being moved from, you can only assign to or destroy other . Any other operation will result in undefined behavior.

该函数在 Qt 5.4 引入。

QPalette:: ~QPalette ()

Destroys the palette.

const QBrush &QPalette:: alternateBase () const

Returns the alternate base brush of the current color group.

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: base () const

Returns the base brush of the current color group.

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: brightText () const

Returns the bright text foreground brush of the current color group.

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: brush ( QPalette::ColorGroup group , QPalette::ColorRole role ) const

Returns the brush in the specified color group , used for the given color role .

另请参阅 color (), setBrush (),和 ColorRole .

const QBrush &QPalette:: brush ( QPalette::ColorRole role ) const

这是重载函数。

Returns the brush that has been set for the given color role in the current ColorGroup .

另请参阅 color (), setBrush (),和 ColorRole .

const QBrush &QPalette:: button () const

Returns the button brush of the current color group.

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: buttonText () const

Returns the button text foreground brush of the current color group.

另请参阅 ColorRole and brush ().

qint64 QPalette:: cacheKey () const

返回的数字标识内容对于此 QPalette 对象。截然不同的 QPalette objects can have the same key if they refer to the same contents.

The cacheKey() will change when the palette is altered.

const QColor &QPalette:: color ( QPalette::ColorGroup group , QPalette::ColorRole role ) const

Returns the color in the specified color group , used for the given color role .

另请参阅 brush (), setColor (),和 ColorRole .

const QColor &QPalette:: color ( QPalette::ColorRole role ) const

这是重载函数。

Returns the color that has been set for the given color role in the current ColorGroup .

另请参阅 brush () 和 ColorRole .

QPalette::ColorGroup QPalette:: currentColorGroup () const

Returns the palette's current color group.

另请参阅 setCurrentColorGroup ().

const QBrush &QPalette:: dark () const

Returns the dark brush of the current color group.

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: highlight () const

Returns the highlight brush of the current color group.

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: highlightedText () const

Returns the highlighted text brush of the current color group.

另请参阅 ColorRole and brush ().

bool QPalette:: isBrushSet ( QPalette::ColorGroup cg , QPalette::ColorRole cr ) const

返回 true ColorGroup cg and ColorRole cr has been set previously on this palette; otherwise returns false .

该函数在 Qt 4.2 引入。

另请参阅 setBrush ().

bool QPalette:: isCopyOf (const QPalette & p ) const

返回 true if this palette and p are copies of each other, i.e. one of them was created as a copy of the other and neither was subsequently modified; otherwise returns false . This is much stricter than equality.

另请参阅 operator= () 和 operator== ().

bool QPalette:: isEqual ( QPalette::ColorGroup cg1 , QPalette::ColorGroup cg2 ) const

返回 true (usually quickly) if color group cg1 等于 cg2 ;否则返回 false .

const QBrush &QPalette:: light () const

Returns the light brush of the current color group.

另请参阅 ColorRole and brush ().

Returns the unvisited link text brush of the current color group.

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: linkVisited () const

Returns the visited link text brush of the current color group.

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: mid () const

Returns the mid brush of the current color group.

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: midlight () const

Returns the midlight brush of the current color group.

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: placeholderText () const

Returns the placeholder text brush of the current color group.

注意: Before Qt 5.12, the placeholder text color was hard-coded in the code as QPalette::text (). color () where an alpha of 128 was applied. We continue to support this behavior by default, unless you set your own brush. One can get back the original placeholder color setting the special QBrush default constructor as placeholder brush.

该函数在 Qt 5.12 引入。

另请参阅 ColorRole and brush ().

QPalette QPalette:: resolve (const QPalette & other ) const

返回新的 QPalette that has attributes copied from other .

void QPalette:: setBrush ( QPalette::ColorRole role , const QBrush & brush )

Sets the brush for the given color role 到指定 brush for all groups in the palette.

另请参阅 brush (), setColor (),和 ColorRole .

void QPalette:: setBrush ( QPalette::ColorGroup group , QPalette::ColorRole role , const QBrush & brush )

这是重载函数。

Sets the brush in the specified color group , used for the given color role ,到 brush .

另请参阅 brush (), setColor (),和 ColorRole .

void QPalette:: setColor ( QPalette::ColorGroup group , QPalette::ColorRole role , const QColor & color )

Sets the color in the specified color group , used for the given color role , to the specified solid color .

另请参阅 setBrush (), color (),和 ColorRole .

void QPalette:: setColor ( QPalette::ColorRole role , const QColor & color )

这是重载函数。

Sets the color used for the given color role , in all color groups, to the specified solid color .

另请参阅 brush (), setColor (),和 ColorRole .

void QPalette:: setColorGroup ( QPalette::ColorGroup cg , const QBrush & windowText , const QBrush & button , const QBrush & light , const QBrush & dark , const QBrush & mid , const QBrush & text , const QBrush & bright_text , const QBrush & base , const QBrush & window )

Sets a the group at cg . You can pass either brushes, pixmaps or plain colors for windowText , button , light , dark , mid , text , bright_text , base and window .

另请参阅 QBrush .

void QPalette:: setCurrentColorGroup ( QPalette::ColorGroup cg )

Set the palette's current color group to cg .

另请参阅 currentColorGroup ().

const QBrush &QPalette:: shadow () const

Returns the shadow brush of the current color group.

另请参阅 ColorRole and brush ().

void QPalette:: swap ( QPalette & other )

Swaps this palette instance with other 。此函数非常快且从不失败。

该函数在 Qt 5.0 引入。

const QBrush &QPalette:: text () const

Returns the text foreground brush of the current color group.

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: toolTipBase () const

Returns the tool tip base brush of the current color group. This brush is used by QToolTip and QWhatsThis .

注意: Tool tips use the Inactive color group of QPalette , because tool tips are not active windows.

该函数在 Qt 4.4 引入。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: toolTipText () const

Returns the tool tip text brush of the current color group. This brush is used by QToolTip and QWhatsThis .

注意: Tool tips use the Inactive color group of QPalette , because tool tips are not active windows.

该函数在 Qt 4.4 引入。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: window () const

Returns the window (general background) brush of the current color group.

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: windowText () const

Returns the window text (general foreground) brush of the current color group.

另请参阅 ColorRole and brush ().

QVariant QPalette:: operator QVariant () const

Returns the palette as a QVariant

bool QPalette:: operator!= (const QPalette & p ) const

返回 true (slowly) if this palette is different from p ;否则返回 false (usually quickly).

注意: 当前 ColorGroup is not taken into account when comparing palettes

另请参阅 operator== ().

QPalette &QPalette:: operator= (const QPalette & p )

赋值 p to this palette and returns a reference to this palette.

This operation is fast thanks to 隐式共享 .

QPalette &QPalette:: operator= ( QPalette && other )

移动赋值 other 到此 QPalette 实例。

该函数在 Qt 5.2 引入。

bool QPalette:: operator== (const QPalette & p ) const

返回 true (usually quickly) if this palette is equal to p ;否则返回 false (slowly).

注意: 当前 ColorGroup is not taken into account when comparing palettes

另请参阅 operator!= ().

相关非成员

QDataStream & operator<< ( QDataStream & s , const QPalette & p )

Writes the palette, p 到流 s 并返回流引用。

另请参阅 QDataStream 运算符格式 .

QDataStream & operator>> ( QDataStream & s , QPalette & p )

Reads a palette from the stream, s into the palette p ,并返回流引用。

另请参阅 QDataStream 运算符格式 .