QPalette

PyQt5.QtGui.QPalette

Description

The QPalette class contains color groups for each widget state.

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:

  • 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() 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 ColorRole documentation.

We strongly recommend that you use the default palette of the current style (returned by 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 implicit sharing, so it is very efficient to pass QPalette objects as arguments.

Warning: 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.

Enums

ColorGroup

Member

Value

Description

Active

TODO

TODO

All

TODO

TODO

Current

TODO

TODO

Disabled

TODO

TODO

Inactive

TODO

TODO

NColorGroups

TODO

TODO

Normal

Active

synonym for Active


ColorRole
../../_images/palette.png

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

The central roles are:

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.

Selected (marked) items have two roles:

There are two color roles related to hyperlinks:

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 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);

Member

Value

Description

AlternateBase

16

Used as the alternate background color in views with alternating row colors (see QAbstractItemView::setAlternatingRowColors()).

Background

Window

This value is obsolete. Use Window instead.

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.

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.

Button

1

The general button background color. This background can be different from Window as some styles require a different background color for buttons.

ButtonText

8

A foreground color used with the Button color.

Dark

4

Darker than Button.

Foreground

WindowText

This value is obsolete. Use instead.

Highlight

12

A color to indicate a selected item or the current item. By default, the highlight color is darkBlue.

HighlightedText

13

A text color that contrasts with Highlight. By default, the highlighted text color is white.

Light

2

Lighter than Button color.

14

A text color used for unvisited hyperlinks. By default, the link color is blue.

LinkVisited

15

A text color used for already visited hyperlinks. By default, the linkvisited color is magenta.

Mid

5

Between Button and Dark.

Midlight

3

Between Button and Light.

NColorRoles

TODO

TODO

NoRole

17

No role; this special role is often used to indicate that a role has not been assigned.

PlaceholderText

TODO

Used as the placeholder color for various text input widgets. This enum value has been introduced in Qt 5.12

Shadow

11

A very dark color. By default, the shadow color is black.

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.

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.

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.

Window

10

A general background color.

WindowText

0

A general foreground color.

Methods

__init__()

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

See also

QApplication::setPalette()QApplication::palette().


__init__(Union[QColor, GlobalColor, QGradient])

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.


__init__(GlobalColor)

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.


__init__(QPalette)

Constructs a copy of p.

This constructor is fast thanks to implicit sharing.


__init__(Any)

TODO


__init__(Union[QColor, GlobalColor, QGradient], Union[QColor, GlobalColor, QGradient])

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


__init__(Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient])

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

See also

QBrush.


alternateBase() → QBrush

TODO


base() → QBrush

TODO


brightText() → QBrush

TODO


brush(ColorRole) → QBrush

TODO


brush(ColorGroup, ColorRole) → QBrush

See also

setBrush().


button() → QBrush

TODO


buttonText() → QBrush

TODO


cacheKey() → int

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 will change when the palette is altered.


color(ColorRole) → QColor

TODO


color(ColorGroup, ColorRole) → QColor

See also

setColor().


currentColorGroup() → ColorGroup

dark() → QBrush

TODO


__eq__(QPalette) → bool

TODO


highlight() → QBrush

TODO


highlightedText() → QBrush

TODO


isBrushSet(ColorGroup, ColorRole) → bool

TODO


isCopyOf(QPalette) → bool

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.

See also

operator=(), operator==().


isEqual(ColorGroup, ColorGroup) → bool

TODO


light() → QBrush

TODO


TODO


linkVisited() → QBrush

TODO


mid() → QBrush

TODO


midlight() → QBrush

TODO


__ne__(QPalette) → bool

TODO


placeholderText() → QBrush

TODO


resolve() → int

TODO


resolve(QPalette) → QPalette

Returns a new QPalette that has attributes copied from other.


resolve(int)

TODO


setBrush(ColorRole, Union[QBrush, QColor, GlobalColor, QGradient])

See also

brush().


setBrush(ColorGroup, ColorRole, Union[QBrush, QColor, GlobalColor, QGradient])

TODO


setColor(ColorRole, Union[QColor, GlobalColor, QGradient])

TODO


setColor(ColorGroup, ColorRole, Union[QColor, GlobalColor, QGradient])

See also

color().


setColorGroup(ColorGroup, Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient], Union[QBrush, QColor, GlobalColor, QGradient])

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.

See also

QBrush.


setCurrentColorGroup(ColorGroup)

shadow() → QBrush

TODO


swap(QPalette)

TODO


text() → QBrush

TODO


toolTipBase() → QBrush

TODO


toolTipText() → QBrush

TODO


window() → QBrush

TODO


windowText() → QBrush

TODO