如何更改QMessageBox确定按钮的颜色和背景颜色?

huangapple go评论73阅读模式
英文:

How to change QMessagebox ok button color and background color?

问题

我想要更改背景颜色和确定按钮颜色。但是,如果我更改背景颜色,按钮颜色会自动更改。

我的代码:

auto msgBox = new QMessageBox();
msgBox->setAttribute(Qt::WA_DeleteOnClose);
msgBox->setMinimumSize(300, 300);
msgBox->setWindowTitle("Error");
msgBox->setIcon(QMessageBox::NoIcon);
msgBox->setStyleSheet("QPushButton{ color: red; background-color: white }");
msgBox->setStyleSheet("background-color: green;");

请注意,要更改背景颜色和按钮颜色,您需要将这两个属性合并到一个setStyleSheet调用中,而不是分成两个调用。

英文:

I want to change background color and ok button color. But If I change background color button color is automatically changed.

My code:

auto msgBox = new QMessageBox();
msgBox->setAttribute(Qt::WA_DeleteOnClose);
msgBox->setMinimumSize(300, 300);
msgBox->setWindowTitle("Error");
msgBox->setIcon(QMessageBox::NoIcon);
msgBox->setStyleSheet("QPushButton{ color: red; background-color: white }");
msgBox->setStyleSheet("background-color:green;");

答案1

得分: 1

Explanation:

确定按钮QPushButton)是QMessageBox的子控件,因此当你设置它的样式表,然后设置QMessageBox的样式表时,按钮的样式表将被覆盖。这可能是因为,在冲突时,父控件的样式表将被应用。所以你需要避免这种冲突,以达到你需要的外观。

Solution 1:

auto msgBox = new QMessageBox();
msgBox->setMinimumSize(300, 300);
msgBox->setStyleSheet("QMessageBox{ background-color: green;}"
                      "QPushButton{ background-color: white; color: red;}");

这是结果:

如何更改QMessageBox确定按钮的颜色和背景颜色?

你需要为QMessageBox指定绿色背景颜色,如果不这样做(你的做法),它将覆盖应用于其子控件的任何其他样式表,即使是在之后应用的。

为了演示,这也不会起作用:

msgBox->setStyleSheet("background-color:green;");
msgBox->styleSheet().append("QPushButton{ color: red; background-color: white }");

它将导致按钮也具有绿色背景。

Solution 2

你可以像你正在做的那样设置msgBox的样式表,然后可以直接更改按钮的样式表,如下所示:

msgBox->setStyleSheet("background-color: green;");
msgBox->button(QMessageBox::Ok)->setStyleSheet("background-color: white; color: red;");

这相当于在样式表中指定对象名称,但在这里你不需要担心,因为样式冲突发生在两个不同的对象之间,一个是QMessageBox,另一个是QPushButton

英文:

Explanation:

The Ok button (a QPushButton) is a child of QMessageBox, so when you set its stylesheet, and then set QMessageBox's stylesheet, the button's stylesheet gets overridden. Probably because, when in conflict, the parent's stylesheet gets set. So you need to avoid that conflict, to achieve the look you need.

Solution 1:

auto msgBox = new QMessageBox();
msgBox->setMinimumSize(300, 300);
msgBox->setStyleSheet("QMessageBox{ background-color: green;}"
                      "QPushButton{ background-color: white; color: red;}");

This is the result:

如何更改QMessageBox确定按钮的颜色和背景颜色?

You need to specify the green background color for QMessageBox, if you don't (the way you're doing it), it will override every other style sheet you apply to any of its children, even if applied after.

To demonstrate, this will also not work:

msgBox->setStyleSheet("background-color:green;");
msgBox->styleSheet().append("QPushButton{ color: red; background-color: white }");

It will result in a green background for the button as well.

Solution 2

You could set your msgBox stylesheet the way you're doing, and for the button, you could change its stylesheet directly as follows:

msgBox->setStyleSheet( "background-color: green;" );
msgBox->button(QMessageBox::Ok)->setStyleSheet( "background-color: white; color: red;" );

This is the equivalent of specifying object names in stylesheet, but you don't need to bother with that here, because the style conflict is between 2 different objects, a QMessageBox, and a QPushButton.

huangapple
  • 本文由 发表于 2023年4月11日 14:46:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定