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 。Qt 中的所有 Widget 都包含调色板,并使用它们的调色板来绘制自己。这使 UI (用户界面) 变得易于配置,且更容易保持一致性。

若创建新的 Widget,强烈推荐使用调色板中的颜色,而不是硬编码特定颜色。

颜色组:

  • Active (活动) 组用于拥有键盘聚焦的窗口。
  • Inactive (不活动) 组用于其它窗口。
  • Disabled (被禁用) 组用于由于某种原因,被禁用的 Widget (而非窗口)。

活动和不活动窗口两者,可以包含禁用 Widget (被禁用 Widget 经常被称为 不可访问 or 变灰 )。

大多数风格,Active 和 Inactive 看起来一样。

可以为调色板的任何颜色组中的特定角色,设置颜色和笔刷采用 setColor () 和 setBrush ()。颜色组包含用于绘制 Widget 自身的一组颜色。推荐 Widget 使用来自调色板的颜色组角色 (譬如:foreground 和 base),而不是像 "红色" 或 "绿松石色" 的文字颜色。颜色角色的枚举和定义在 ColorRole 文档编制。

强烈推荐使用当前风格的默认调色板 (返回通过 QGuiApplication::palette ()) 并按需求修改。这由 Qt 小部件完成的,当绘制它们时。

要修改颜色组,调用函数 setColor () 和 setBrush (),从属想要纯色,或像素图模式。

还有相应的 color () 和 brush () getter,和常用方便函数以获取 ColorRole 对于当前 ColorGroup : window (), windowText (), base (),等。

可以使用拷贝构造函数拷贝调色板,并测试看 2 调色板是否 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 Active 的同义词

enum QPalette:: ColorRole

Color Roles

ColorRole 枚举定义当前 GUI 中使用的不同符号颜色角色。

中心角色包括:

常量 描述
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 主要用作文本录入 Widget 的背景色,但也可用于其它描绘 (譬如:组合框下拉列表和工具栏手柄的背景)。它通常为白色,或另一浅色。
QPalette::AlternateBase 16 在交替行颜色的视图中用作替代背景色 (见 QAbstractItemView::setAlternatingRowColors ()).
QPalette::ToolTipBase 18 用作背景色对于 QToolTip and QWhatsThis 。工具提示使用 Inactive (不活动) 颜色组对于 QPalette ,因为工具提示不是活动窗口。
QPalette::ToolTipText 19 用作前景颜色对于 QToolTip and QWhatsThis 。工具提示使用 Inactive (不活动) 颜色组对于 QPalette ,因为工具提示不是活动窗口。
QPalette::PlaceholderText 20 用作各种文本输入 Widget 的占位符颜色。该枚举值在 Qt 5.12 引入
QPalette::Text 6 前景颜色使用 。这通常如同 WindowText ,在这种情况下,它必须提供很好的对比与 Window and .
QPalette::Button 1 一般按钮的背景色。此背景可以不同于 Window 因为有些样式要求不同背景色对于按钮。
QPalette::ButtonText 8 前景颜色使用 Button 颜色。
QPalette::BrightText 7 文本颜色很不同于 WindowText ,且有鲜明对比与 Dark 。通常用于需要绘制的文本,其中 文本 or WindowText 会给出较差的对比 (譬如:按下按钮)。注意,文本颜色可以用于除仅单词外的东西;文本颜色 usually 用于文本,但线条、图标、等使用文本颜色角色也非常常见。

有一些颜色角色主要用于 3D 斜角和阴影效果。所有这些通常派生自 Window ,并以依赖这种关系的方式被使用。例如,按钮依赖它以使斜角看起来很有吸引力,和 Motif 滚动条依赖于 Mid 稍微不同于 Window .

常量 描述
QPalette::Light 2 更亮比 Button 颜色。
QPalette::Midlight 3 介于 Button and Light .
QPalette::Dark 4 更暗比 Button .
QPalette::Mid 5 介于 Button and Dark .
QPalette::Shadow 11 非常深的颜色。默认情况下,阴影颜色为 Qt::black .

被选中 (标记) 的项有 2 种角色:

常量 描述
QPalette::Highlight 12 指示选定项 (或当前项) 的颜色。默认情况下,高亮颜色为 Qt::darkBlue .
QPalette::HighlightedText 13 文本颜色形成对比与 Highlight 。默认情况下,高亮文本颜色为 Qt::white .

有 2 超链接相关颜色角色:

常量 描述
QPalette::Link 14 用于未拜访超链接的文本颜色。默认情况下,链接颜色为 Qt::blue .
QPalette::LinkVisited 15 用于已拜访超链接的文本颜色。默认情况下,链接拜访颜色为 Qt::magenta .

注意,不要使用 Link and LinkVisited 角色,当在 Qt 中渲染富文本时,推荐使用 CSS 和 QTextDocument::setDefaultStyleSheet () 函数来更改链接的外观。例如:

    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 没有角色;此特殊角色经常用于指示,没有赋值角色。

成员函数文档编制

QPalette:: QPalette ()

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

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

QPalette:: QPalette (const QColor & button )

构造调色板从 button 颜色。其它颜色是自动计算的,基于此颜色。 Window 也会是按钮颜色。

QPalette:: QPalette ( Qt::GlobalColor button )

构造调色板从 button 颜色。其它颜色是自动计算的,基于此颜色。 Window 也会是按钮颜色。

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

构造调色板从 button 颜色和 window 。其它颜色是基于这些颜色自动计算的。

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 )

构造调色板。可以传递画笔、像素图、或纯色为 windowText , button , light , dark , mid , text , bright_text , base and window .

另请参阅 QBrush .

QPalette:: QPalette (const QPalette & p )

构造副本为 p .

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

QPalette:: QPalette ( QPalette && other )

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

在被移动后,只可以赋值给或销毁 other 。任何其它操作都将导致未定义行为。

该函数在 Qt 5.4 引入。

QPalette:: ~QPalette ()

销毁调色板。

const QBrush &QPalette:: alternateBase () const

返回当前颜色组的替代基笔刷。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: base () const

返回当前颜色组的基画笔。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: brightText () const

返回当前颜色组的亮文本前景笔刷。

另请参阅 ColorRole and brush ().

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

返回笔刷按指定颜色 group ,用于给定颜色 role .

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

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

这是重载函数。

返回已设置的笔刷为给定颜色 role 在当前 ColorGroup .

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

const QBrush &QPalette:: button () const

返回当前颜色组的按钮笔刷。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: buttonText () const

返回当前颜色组的按钮文本前景笔刷。

另请参阅 ColorRole and brush ().

qint64 QPalette:: cacheKey () const

返回内容标识数对于此 QPalette 对象。截然不同的 QPalette 对象可以拥有相同键,若它们引用相同内容。

cacheKey() 会改变,当变更调色板时。

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

返回颜色按指定颜色 group ,用于给定颜色 role .

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

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

这是重载函数。

返回已设置的颜色为给定颜色 role 在当前 ColorGroup .

另请参阅 brush () 和 ColorRole .

QPalette::ColorGroup QPalette:: currentColorGroup () const

返回调色板的当前颜色组。

另请参阅 setCurrentColorGroup ().

const QBrush &QPalette:: dark () const

返回当前颜色组的深色笔刷。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: highlight () const

返回当前颜色组的高亮笔刷。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: highlightedText () const

返回当前颜色组的高亮文本笔刷。

另请参阅 ColorRole and brush ().

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

返回 true ColorGroup cg and ColorRole cr 先前在此调色板中有设置;否则返回 false .

该函数在 Qt 4.2 引入。

另请参阅 setBrush ().

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

返回 true 若此调色板和 p 是彼此的副本 (即:它们中的一个是作为其它的副本创建的,且随后未修改);否则返回 false 。这比相等严格得多。

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

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

返回 true (通常很快) 若颜色组 cg1 等于 cg2 ;否则返回 false .

const QBrush &QPalette:: light () const

返回当前颜色组的浅色笔刷。

另请参阅 ColorRole and brush ().

返回当前颜色组未拜访的链接文本笔刷。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: linkVisited () const

返回当前颜色组拜访的链接文本笔刷。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: mid () const

返回当前颜色组的中间笔刷。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: midlight () const

返回当前颜色组的中光笔刷。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: placeholderText () const

返回当前颜色组的占位符文本笔刷。

注意: 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 )

设置笔刷为给定颜色 role 到指定 brush 为调色板中的所有组。

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

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

这是重载函数。

设置笔刷按指定颜色 group ,用于给定颜色 role ,到 brush .

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

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

设置颜色按指定颜色 group ,用于给定颜色 role ,到指定纯 color .

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

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

这是重载函数。

设置的颜色用于给定颜色 role ,在所有颜色组,到指定纯 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 )

设置组按 cg 。可以传递画笔、像素图、或纯色为 windowText , button , light , dark , mid , text , bright_text , base and window .

另请参阅 QBrush .

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

把调色板的当前颜色组设为 cg .

另请参阅 currentColorGroup ().

const QBrush &QPalette:: shadow () const

返回当前颜色组的阴影笔刷。

另请参阅 ColorRole and brush ().

void QPalette:: swap ( QPalette & other )

交换此调色板实例与 other 。此函数非常快且从不失败。

该函数在 Qt 5.0 引入。

const QBrush &QPalette:: text () const

返回当前颜色组的文本前景笔刷。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: toolTipBase () const

返回当前颜色组的工具提示基笔刷。此笔刷用于 QToolTip and QWhatsThis .

注意: 工具提示使用 Inactive (不活动) 颜色组对于 QPalette ,因为工具提示不是活动窗口。

该函数在 Qt 4.4 引入。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: toolTipText () const

返回当前颜色组的工具提示文本笔刷。此笔刷用于 QToolTip and QWhatsThis .

注意: 工具提示使用 Inactive (不活动) 颜色组对于 QPalette ,因为工具提示不是活动窗口。

该函数在 Qt 4.4 引入。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: window () const

返回当前颜色组的窗口 (一般背景) 笔刷。

另请参阅 ColorRole and brush ().

const QBrush &QPalette:: windowText () const

返回当前颜色组的窗口文本 (一般前景) 笔刷。

另请参阅 ColorRole and brush ().

QVariant QPalette:: operator QVariant () const

返回调色板作为 QVariant

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

返回 true (很慢) 若此调色板不同于 p ;否则返回 false (通常很快)。

注意: 当前 ColorGroup 不考虑,当比较调色板时

另请参阅 operator== ().

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

赋值 p 到此调色板,并返回此调色板的引用。

此操作很快,感谢 隐式共享 .

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

移动赋值 other 到此 QPalette 实例。

该函数在 Qt 5.2 引入。

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

返回 true (通常很快) 若此调色板等于 p ;否则返回 false (很慢)。

注意: 当前 ColorGroup 不考虑,当比较调色板时

另请参阅 operator!= ().

相关非成员

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

写入调色板 p 到流 s 并返回流引用。

另请参阅 QDataStream 运算符格式 .

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

读取调色板从流 s 到调色板 p ,并返回流引用。

另请参阅 QDataStream 运算符格式 .