对话框(03):【类】QColorDialog [官翻]

QColorDialog Class

QColorDialog类提供了一个用于指定颜色的对话框小部件。

Header#include < QColorDialog >
qmakeQT += widgets
InheritsQDialog
Inherited By

详细说明

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

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

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

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

当按下 “Pick Screen Color” 按钮时,光标变为十字形,并且扫描屏幕上的颜色。用户可以通过单击鼠标或Enter按钮来选择一个。按Escape可以恢复进入此模式之前选择的最后一种颜色。

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

A color dialog in the Fusion widget style.

公共类型

enum ColorDialogOption

flags ColorDialogOptions

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

ConstantValueDescription
ShowAlphaChannel0x00000001允许用户选择颜色的alpha分量。
NoButtons0x00000002不显示“确定”和“取消”按钮。 (对于“live dialogs”很有用。)
DontUseNativeDialog0x00000004使用Qt的标准颜色对话框而不是操作系统本机颜色对话框。

属性

  1. currentColor: QColor 在对话框中保留的当前选择的颜色

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

    Notifier signal:

    • void currentColorChanged(const QColor &color)
  2. options: ColorDialogOptions 包含影响对话框外观的各种选项

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

    在显示对话框之前,应先设置选项。 在对话框可见时进行设置并不能保证对对话框具有立竿见影的效果(取决于选项和平台)。

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

公共函数

构造和析构

  • QColorDialog(const QColor &initial, QWidget *parent = nullptr)
  • QColorDialog(QWidget *parent = nullptr)
  • virtual ~QColorDialog()

属性相关

  • QColor currentColor() const
    void setCurrentColor(const QColor &color)
  • QColorDialog::ColorDialogOptions options() const
    void setOptions(QColorDialog::ColorDialogOptions options)

其它

  • void open(QObject *receiver, const char *member)
    打开对话框并将其colorSelected() 信号连接到接收者和成员指定的插槽
    关闭对话框后,信号将从插槽中断开。
  • QColor selectedColor() const 用户通过单击“确定”或等效按钮选择的颜色
    注意:此颜色并不总是与currentColor属性所保持的颜色相同,因为用户可以在最终选择要使用的一种颜色之前选择其他颜色。
  • bool testOption(QColorDialog::ColorDialogOption option) const
    void setOption(QColorDialog::ColorDialogOption option, bool on = true)

重写的公共函数

  • virtual void setVisible(bool visible) override

信号

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

静态的公共函数

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

重写的受保护的函数

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

演示代码

#include <QtWidgets>
#define WidgetType QColorDialog
int i=0,j=0;
QList<WidgetType *> widgetList;
QList<QLabel *> labelList;

QCommonStyle cs;

void addFrame(QGridLayout *mainLayout,QWidget *parent){
    WidgetType *widget = new WidgetType(parent);
    widgetList.append (widget);

    QLabel *label = new QLabel(parent);
    label->setAlignment (Qt::AlignTop);
    labelList.append (label);

    auto *frame = new QFrame(parent);
    frame->setFrameStyle (QFrame::Panel | QFrame::Plain);

    auto *vBox = new QVBoxLayout(frame);
    vBox->addWidget (label,Qt::AlignCenter);
    vBox->addWidget (widget,Qt::AlignCenter);
    mainLayout->addWidget (frame,i,j);
    j++;
}

void setLabelText(int i,QString text){
    if(i >= labelList.count ()){
        return;
    }
    QLabel *label = labelList.at (i);
    label->setText (text);
}

WidgetType* getWidget(int i){
    if(i >= widgetList.count ()){
        qDebug() <<   __LINE__ <<"行 getWidget(int i) 函数 变量"<<    i  << "超出范围";
        i = 0;          // 超出范围默认使用 0
    }
    return  widgetList.at (i);
}

int main(int argc, char *argv[])
{
    QApplication app(argc,argv);

    QMetaObject mo = WidgetType::staticMetaObject;
    app.setApplicationName (mo.className ());

    QDialog w;
    auto *mainLayout = new QGridLayout(&w);

    addFrame (mainLayout,&w);
    addFrame (mainLayout,&w);
    addFrame (mainLayout,&w);

    getWidget (0)->setOption (QColorDialog::ShowAlphaChannel);
    getWidget (1)->setOption (QColorDialog::NoButtons);
    getWidget (2)->setOption (QColorDialog::DontUseNativeDialog);

    setLabelText (0,"<p style='color:red;font-size:20px;'>QColorDialog::ShowAlphaChannel</p>");
    setLabelText (1,"<p style='color:red;font-size:20px;'>QColorDialog::NoButtons</p>");
    setLabelText (2,"<p style='color:red;font-size:20px;'>QColorDialog::DontUseNativeDialog</p>");

    w.show ();
    app.exec();
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值