QtBase
v6.3.1
|
The QPalette class contains color groups for each widget state. More...
#include <qpalette.h>
Public Types | |
enum | ColorGroup { Active , Disabled , Inactive , NColorGroups , Current , All , Normal = Active } |
enum | ColorRole { WindowText , Button , Light , Midlight , Dark , Mid , Text , BrightText , ButtonText , Base , Window , Shadow , Highlight , HighlightedText , Link , LinkVisited , AlternateBase , NoRole , ToolTipBase , ToolTipText , PlaceholderText , NColorRoles = PlaceholderText + 1 } |
using | ResolveMask = quint64 |
Friends | |
Q_GUI_EXPORT QDataStream & | operator<< (QDataStream &s, const QPalette &p) |
Related Functions | |
(Note that these are not member functions.) | |
QDataStream & | operator<< (QDataStream &s, const QPalette &p) |
QDataStream & | operator>> (QDataStream &s, QPalette &p) |
The QPalette class contains color groups for each widget state.
\inmodule QtGui
A palette consists of three color groups: Active, Disabled, and 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: \list
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() and 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 \l ColorRole documentation.
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() and setBrush(), depending on whether you want a pure color or a pixmap pattern.
There are also corresponding color() and brush() getters, and a commonly used convenience function to get the ColorRole for the current ColorGroup: window(), windowText(), base(), etc.
You can copy a palette using the copy constructor and test to see if two palettes are identical using isCopyOf().
QPalette is optimized by the use of \l{implicit sharing}, so it is very efficient to pass QPalette objects as arguments.
Definition at line 54 of file qpalette.h.
using QPalette::ResolveMask = quint64 |
Definition at line 146 of file qpalette.h.
enum QPalette::ColorGroup |
\value Disabled \value Active \value Inactive \value Normal synonym for Active
\omitvalue All \omitvalue NColorGroups \omitvalue Current
Enumerator | |
---|---|
Active | |
Disabled | |
Inactive | |
NColorGroups | |
Current | |
All | |
Normal |
Definition at line 84 of file qpalette.h.
enum QPalette::ColorRole |
Color Roles
The ColorRole enum defines the different symbolic color roles used in current GUIs.
The central roles are:
\value Window A general background color.
\value WindowText A general foreground color.
\value Base 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.
\value AlternateBase Used as the alternate background color in views with alternating row colors (see QAbstractItemView::setAlternatingRowColors()).
\value ToolTipBase 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.
\value ToolTipText 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.
\value PlaceholderText Used as the placeholder color for various text input widgets. This enum value has been introduced in Qt 5.12
\value Text 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
.
\value Button The general button background color. This background can be different from Window
as some styles require a different background color for buttons.
\value ButtonText A foreground color used with the Button
color.
\value BrightText 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.
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
.
\value Light Lighter than Button
color.
\value Midlight Between Button
and Light
.
\value Dark Darker than Button
.
\value Mid Between Button
and Dark
.
\value Shadow A very dark color. By default, the shadow color is Qt::black.
Selected (marked) items have two roles:
\value Highlight A color to indicate a selected item or the current item. By default, the highlight color is Qt::darkBlue.
\value HighlightedText A text color that contrasts with Highlight
. By default, the highlighted text color is Qt::white.
There are two color roles related to hyperlinks:
\value Link A text color used for unvisited hyperlinks. By default, the link color is Qt::blue.
\value LinkVisited 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:
\value NoRole No role; this special role is often used to indicate that a role has not been assigned.
\omitvalue NColorRoles
Definition at line 86 of file qpalette.h.
QPalette::QPalette | ( | ) |
Constructs an empty palette object with no color roles set.
When used as the palette of a QWidget the colors are resolved as described by QWidget::setPalette().
Definition at line 540 of file qpalette.cpp.
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.
Definition at line 562 of file qpalette.cpp.
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.
Definition at line 573 of file qpalette.cpp.
Constructs a palette from a button color and a window. The other colors are automatically calculated, based on these colors.
Definition at line 622 of file qpalette.cpp.
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.
Definition at line 586 of file qpalette.cpp.
QPalette::QPalette | ( | const QColor & | windowText, |
const QColor & | window, | ||
const QColor & | light, | ||
const QColor & | dark, | ||
const QColor & | mid, | ||
const QColor & | text, | ||
const QColor & | base | ||
) |
Constructs a palette with the specified windowText, window, light, dark, mid, text, and base colors. The button color will be set to the window color.
Definition at line 605 of file qpalette.cpp.
Constructs a copy of p.
This constructor is fast thanks to \l{implicit sharing}.
Definition at line 657 of file qpalette.cpp.
QPalette::~QPalette | ( | ) |
Destroys the palette.
Definition at line 677 of file qpalette.cpp.
|
inlinenoexcept |
Move-constructs a QPalette instance, making it point at the same object that other was pointing to.
After being moved from, you can only assign to or destroy other. Any other operation will result in undefined behavior.
Definition at line 70 of file qpalette.h.
Returns the alternate base brush of the current color group.
Definition at line 124 of file qpalette.h.
Returns the base brush of the current color group.
Definition at line 123 of file qpalette.h.
Returns the bright text foreground brush of the current color group.
Definition at line 129 of file qpalette.h.
const QBrush & QPalette::brush | ( | ColorGroup | group, |
ColorRole | role | ||
) | const |
Returns the brush in the specified color group, used for the given color role.
Definition at line 738 of file qpalette.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns the brush that has been set for the given color role in the current ColorGroup.
Definition at line 116 of file qpalette.h.
Returns the button brush of the current color group.
Definition at line 118 of file qpalette.h.
Returns the button text foreground brush of the current color group.
Definition at line 130 of file qpalette.h.
qint64 QPalette::cacheKey | ( | ) | const |
Returns a number that identifies the contents of this QPalette object. Distinct QPalette objects can have the same key if they refer to the same contents.
The cacheKey() will change when the palette is altered.
Definition at line 919 of file qpalette.cpp.
|
inline |
Returns the color in the specified color group, used for the given color role.
Definition at line 101 of file qpalette.h.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns the color that has been set for the given color role in the current ColorGroup.
Definition at line 115 of file qpalette.h.
|
inline |
Returns the palette's current color group.
Definition at line 98 of file qpalette.h.
Returns the dark brush of the current color group.
Definition at line 120 of file qpalette.h.
Returns the highlight brush of the current color group.
Definition at line 132 of file qpalette.h.
Returns the highlighted text brush of the current color group.
Definition at line 133 of file qpalette.h.
bool QPalette::isBrushSet | ( | ColorGroup | cg, |
ColorRole | cr | ||
) | const |
Returns true
if the ColorGroup cg and ColorRole cr has been set previously on this palette; otherwise returns false
.
The ColorGroup cg should be less than QPalette::NColorGroups, but you can use QPalette::Current. In this case, the previously set current color group will be used.
The ColorRole cr should be less than QPalette::NColorRoles.
Definition at line 808 of file qpalette.cpp.
Returns 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.
Definition at line 1081 of file qpalette.cpp.
bool QPalette::isEqual | ( | QPalette::ColorGroup | group1, |
QPalette::ColorGroup | group2 | ||
) | const |
Returns true
(usually quickly) if color group cg1 is equal to cg2; otherwise returns false
.
Definition at line 885 of file qpalette.cpp.
Returns the light brush of the current color group.
Definition at line 119 of file qpalette.h.
Returns the unvisited link text brush of the current color group.
Definition at line 134 of file qpalette.h.
Returns the visited link text brush of the current color group.
Definition at line 135 of file qpalette.h.
Returns the mid brush of the current color group.
Definition at line 121 of file qpalette.h.
Returns the midlight brush of the current color group.
Definition at line 128 of file qpalette.h.
QPalette::operator QVariant | ( | ) | const |
Returns the palette as a QVariant
Definition at line 716 of file qpalette.cpp.
|
inline |
Returns true
(slowly) if this palette is different from p; otherwise returns false
(usually quickly).
Definition at line 139 of file qpalette.h.
Move-assigns other to this QPalette instance.
Assigns p to this palette and returns a reference to this palette.
This operation is fast thanks to \l{implicit sharing}.
Definition at line 695 of file qpalette.cpp.
Returns true
(usually quickly) if this palette is equal to p; otherwise returns false
(slowly).
Definition at line 866 of file qpalette.cpp.
Returns the placeholder text brush of the current color group.
Definition at line 136 of file qpalette.h.
Returns a new QPalette that is a union of this instance and other. Color roles set in this instance take precedence.
Definition at line 928 of file qpalette.cpp.
QPalette::ResolveMask QPalette::resolveMask | ( | ) | const |
void QPalette::setBrush | ( | ColorGroup | group, |
ColorRole | role, | ||
const QBrush & | brush | ||
) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Sets the brush in the specified color group, used for the given color role, to brush.
Definition at line 770 of file qpalette.cpp.
Sets the brush for the given color role to the specified brush for all groups in the palette.
Definition at line 184 of file qpalette.h.
|
inline |
Sets the color in the specified color group, used for the given color role, to the specified solid color.
Definition at line 179 of file qpalette.h.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Sets the color used for the given color role, in all color groups, to the specified solid color.
Definition at line 182 of file qpalette.h.
void QPalette::setColorGroup | ( | 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.
Definition at line 1094 of file qpalette.cpp.
|
inline |
Set the palette's current color group to cg.
Definition at line 99 of file qpalette.h.
void QPalette::setResolveMask | ( | QPalette::ResolveMask | mask | ) |
Returns the shadow brush of the current color group.
Definition at line 131 of file qpalette.h.
Swaps this palette instance with other. This function is very fast and never fails.
Definition at line 75 of file qpalette.h.
Returns the text foreground brush of the current color group.
Definition at line 122 of file qpalette.h.
Returns the tool tip base brush of the current color group. This brush is used by QToolTip and QWhatsThis.
Definition at line 125 of file qpalette.h.
Returns the tool tip text brush of the current color group. This brush is used by QToolTip and QWhatsThis.
Definition at line 126 of file qpalette.h.
Returns the window (general background) brush of the current color group.
Definition at line 127 of file qpalette.h.
Returns the window text (general foreground) brush of the current color group.
Definition at line 117 of file qpalette.h.
|
related |
Writes the palette, p to the stream s and returns a reference to the stream.
Definition at line 1000 of file qpalette.cpp.
|
friend |
Writes the palette, p to the stream s and returns a reference to the stream.
Definition at line 1000 of file qpalette.cpp.
|
related |
Reads a palette from the stream, s into the palette p, and returns a reference to the stream.
Definition at line 1040 of file qpalette.cpp.