【问题标题】:QDialogButtonBox: Is there any way to fill different colors to Individual Buttons?QDialogBu​​ttonBox:有没有办法为单个按钮填充不同的颜色?
【发布时间】:2012-10-23 05:02:35
【问题描述】:

我正在为我的屏幕创建一个错误消息框。我正在使用 QDialogBu​​ttonBox 作为按钮。 现在我想用不同的颜色填充按钮。 例如: “好的” --> 绿色 “取消”-> 红色等。 我可以更改所有按钮的背景,但不能单独更改。

有没有办法做到这一点?

提前谢谢!!!!

【问题讨论】:

    标签: qt qt4 qmessagebox


    【解决方案1】:

    试试这个(使用 QDialogBu​​ttonBox::button() 和 QPushButton::setStyleSheet())。

    QDialogButtonBox* buttonBox = new QDialogButtonBox;
    // set up your button box
    QColor okButtonColor = Qt::red;
    buttonBox->button(QDialogButtonBox::Ok)->setStyleSheet(QString("background:%1").arg(okButtonColor.name()));
    

    已编辑:在构建样式表字符串周围的代码中修复了错字。

    【讨论】:

    • 感谢您的回复。我试过你的方法。但它没有提供任何超出按钮的东西。在我的情况下: ui->buttonBox->addButton(QDialogBu​​ttonBox::Ok) ui->buttonBox->button(QDialogBu​​ttonBox::Ok); // 如果我尝试输入 (./->) 它什么也没有显示。你能不能给我更多的提示。你的想法似乎不错。
    • 该函数返回 QPushButton * 。所以,可能你需要包含 QPushButton。 buttonBox->button(QDialogBu​​ttonBox::Ok)->setStyleSheet(QString("background:%s").tr(okButtonColor.name()));应该工作。
    【解决方案2】:

    我知道我迟到了,但我会把这个留给遇到与我相同问题的开发人员,这适用于 python 和 c++ 开发人员,您可以使用它来更改特定按钮的颜色:

    #<QDialogButtonBox ObjectName> *[text="Save"] {
        background: red;
    }
    #<QDialogButtonBox ObjectName> *[text="OK"] {
        background: green;
    }
    

    Example on Qt Designer

    【讨论】:

      猜你喜欢
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      相关资源
      最近更新 更多