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

skg*_*skg 0 qt qt4 qmessagebox

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

有没有办法做到这一点?

提前致谢 !!!!

Joo*_*wan 5

试试这个(使用 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()));
Run Code Online (Sandbox Code Playgroud)

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

  • 该函数返回 QPushButton * 。因此,您可能需要包含 QPushButton。buttonBox->button(QDialogBu​​ttonBox::Ok)->setStyleSheet(QString("background:%s").tr(okButtonColor.name())); 应该管用。 (3认同)