QColorDialog Class

QColorDialog 类提供了一个用于指定颜色的对话窗口部件。更多

头文件: #include <QColorDialog>
CMake.QColorDialog find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QDialog

公共类型

enum ColorDialogOption { ShowAlphaChannel, NoButtons, NoEyeDropperButton, DontUseNativeDialog }
flags ColorDialogOptions

属性

公共函数

QColorDialog(QWidget *parent = nullptr)
QColorDialog(const QColor &initial, QWidget *parent = nullptr)
virtual ~QColorDialog()
QColor currentColor() const
void open(QObject *receiver, const char *member)
QColorDialog::ColorDialogOptions options() const
QColor selectedColor() const
void setCurrentColor(const QColor &color)
void setOption(QColorDialog::ColorDialogOption option, bool on = true)
void setOptions(QColorDialog::ColorDialogOptions options)
bool testOption(QColorDialog::ColorDialogOption option) const

重新实现的公共函数

virtual void setVisible(bool visible) override

信号

void colorSelected(const QColor &color)
void currentColorChanged(const QColor &color)

静态公共成员

QColor customColor(int index)
int customCount()
QColor getColor(const QColor &initial = Qt::white, QWidget *parent = nullptr, const QString &title = QString(), QColorDialog::ColorDialogOptions options = ColorDialogOptions())
void setCustomColor(int index, QColor color)
void setStandardColor(int index, QColor color)
QColor standardColor(int index)

重新实现的受保护函数

virtual void changeEvent(QEvent *e) override
virtual void done(int result) override

详细说明

颜色对话框的功能是允许用户选择颜色。例如,您可以在绘图程序中使用它来让用户设置画笔的颜色。

静态函数提供模式颜色对话框。

静态getColor() 函数会显示对话框,并允许用户指定颜色。该函数还可用于让用户选择具有一定透明度的颜色:将ShowAlphaChannel 选项作为附加参数传递给用户。

用户可以存储customCount() 不同的自定义颜色。自定义颜色由所有颜色对话框共享,并在程序执行期间被记住。使用setCustomColor() 设置自定义颜色,使用customColor() 获取自定义颜色。

按下 "选取屏幕颜色 "按钮后,光标会变成一个十字形,屏幕上的颜色会被扫描。用户可以通过点击鼠标或 Enter 按钮来选择其中一种。按 Escape 键可恢复进入此模式前最后选择的颜色。

标准对话框示例展示了如何使用 QColorDialog 以及其他内置 Qt 对话框。

Fusion widget 风格的颜色对话框。

另请参阅 QColor,QFileDialog,QFontDialog标准对话框示例

成员类型文档

枚举 QColorDialog::ColorDialogOption
flags QColorDialog::ColorDialogOptions

该枚举指定了影响颜色对话框外观的各种选项。

常量说明
QColorDialog::ShowAlphaChannel0x00000001允许用户选择颜色的 alpha 分量。
QColorDialog::NoButtons0x00000002不显示OKCancel 按钮。(适用于 "实时对话框")。
QColorDialog::NoEyeDropperButton0x00000008隐藏Eye Dropper 按钮。此值在 Qt 6.6 中添加。
QColorDialog::DontUseNativeDialog0x00000004使用 Qt 的标准颜色对话框,而不是操作系统的本地颜色对话框。

ColorDialogOptions 类型是QFlags<ColorDialogOption> 的类型定义。它存储了 ColorDialogOption 值的 OR 组合。

另请参阅 options,setOption(),testOption() 和windowModality() 。

属性文档

currentColor : QColor

此属性保存对话框中当前选择的颜色

访问功能:

QColor currentColor() const
void setCurrentColor(const QColor &color)

通知信号

void currentColorChanged(const QColor &color)

options : ColorDialogOptions

该属性包含影响对话框外观的各种选项

默认情况下,所有选项都是禁用的。

应在显示对话框之前设置选项。在对话框可见时设置这些选项,并不能保证立即对对话框产生影响(取决于选项和平台)。

访问功能:

QColorDialog::ColorDialogOptions options() const
void setOptions(QColorDialog::ColorDialogOptions options)

另请参阅 setOption() 和testOption()。

成员函数文档

[explicit] QColorDialog::QColorDialog(QWidget *parent = nullptr)

用给定的parent 构建颜色对话框。

[explicit] QColorDialog::QColorDialog(const QColor &initial, QWidget *parent = nullptr)

用给定的parent 和指定的initial 颜色构建颜色对话框。

[virtual noexcept] QColorDialog::~QColorDialog()

销毁颜色对话框。

[override virtual protected] void QColorDialog::changeEvent(QEvent *e)

重实现:QWidget::changeEvent(QEvent *event).

[signal] void QColorDialog::colorSelected(const QColor &color)

该信号在用户点击OK 选择要使用的颜色后发出。所选颜色由color 指定。

另请参阅 colorcurrentColorChanged()。

[signal] void QColorDialog::currentColorChanged(const QColor &color)

每当对话框中的当前颜色发生变化时,就会发出该信号。当前颜色由color 指定。

注: 属性currentColor 的通知信号。

另请参阅 colorcolorSelected()。

[static] QColor QColorDialog::customColor(int index)

QColor 值返回给定index 的自定义颜色。

另请参阅 setCustomColor()。

[static] int QColorDialog::customCount()

返回QColorDialog 支持的自定义颜色的数量。所有颜色对话框共享相同的自定义颜色。

[override virtual protected] void QColorDialog::done(int result)

重实现:QDialog::done(int r)。

关闭对话框并将其结果代码设为result 。如果使用exec() 显示此对话框,done() 将导致本地事件循环结束,而exec() 将返回result

另请参见 QDialog::done()。

[static] QColor QColorDialog::getColor(const QColor &initial = Qt::white, QWidget *parent = nullptr, const QString &title = QString(), QColorDialog::ColorDialogOptions options = ColorDialogOptions())

使用给定的窗口title (如果没有指定,则使用 "选择颜色")弹出模式颜色对话框,让用户选择颜色,并返回该颜色。颜色初始设置为initial 。该对话框是parent 的子对话框。如果用户取消对话框,它将返回无效颜色(参见QColor::isValid() )。

options 参数允许您自定义对话框。

void QColorDialog::open(QObject *receiver, const char *member)

打开对话框,并将colorSelected() 信号连接到receivermember 指定的插槽。

关闭对话框后,信号将从插槽中断开。

QColor QColorDialog::selectedColor() const

返回用户通过点击OK 或同等按钮选择的颜色。

注意: 该颜色并不总是与currentColor 属性持有的颜色相同,因为用户在最终选择要使用的颜色之前可以选择不同的颜色。

[static] void QColorDialog::setCustomColor(int index, QColor color)

index 上的自定义颜色设置为QColor color 值。

注意: 此功能不适用于 macOS 平台上的本地颜色对话框。如果仍需要此功能,请使用QColorDialog::DontUseNativeDialog 选项。

另请参阅 customColor()。

void QColorDialog::setOption(QColorDialog::ColorDialogOption option, bool on = true)

如果on 为 true,则设置option 为启用状态;否则,清除给定的option

另请参阅 optionstestOption()。

[static] void QColorDialog::setStandardColor(int index, QColor color)

index 的标准颜色设置为QColor color 值。

注意: 此功能不适用于 macOS 平台上的本地颜色对话框。如果仍需要此功能,请使用QColorDialog::DontUseNativeDialog 选项。

另请参阅 standardColor()。

[override virtual] void QColorDialog::setVisible(bool visible)

重实现:QDialog::setVisible(bool visible)。

更改对话框的可见性。如果visible 为 true,则显示对话框;否则,隐藏对话框。

[static] QColor QColorDialog::standardColor(int index)

QColor 值返回给定index 处的标准颜色。

另请参阅 setStandardColor()。

bool QColorDialog::testOption(QColorDialog::ColorDialogOption option) const

如果给定的option 已启用,则返回true ;否则返回 false。

另请参阅 optionssetOption()。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.