QDialogBu​​ttonBox:有没有办法为个别按钮填充不同的颜色?

时间:2012-10-23 05:02:36

标签: qt qt4 qmessagebox

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

有没有办法做到这一点?

提前致谢!!!!

2 个答案:

答案 0 :(得分:4)

试试这个(使用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()));

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

答案 1 :(得分:0)

我知道我来晚了,但是我要把这个留给一个像我一样经历过同样问题的开发人员,这对python和c ++开发人员都适用,您可以使用它来更改特定按钮的颜色:

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

Example on Qt Designer

相关问题