我想改变背景颜色和确定按钮颜色。但是如果我改变背景颜色按钮颜色会自动改变。
我的代码:
auto msgBox = new MsgBox(rootWidget);
msgBox->setAttribute(Qt::WA_DeleteOnClose);
msgBox->setMinimumSize(300, 300);
msgBox->setWindowTitle(Common::appName());
msgBox->setIcon(QMessageBox::NoIcon);
msgBox->setStyleSheet("QPushButton{ color: red; background-color: white }");
msgBox->setStyleSheet("background-color:green;");
正如评论中已经提到的,您正在覆盖样式表,为避免这种情况,您可以执行以下操作:
auto msgBox = new QMessageBox();
msgBox->setMinimumSize(300, 300);
msgBox->setStyleSheet("QMessageBox"
"{"
"background-color: green;"
"}"
"QPushButton"
"{"
"background-color: white; color: red;"
"}");
这是结果:
您需要指定绿色背景颜色用于
QMessageBox
,如果您不这样做(您这样做的方式),它将覆盖您应用到它的所有其他样式表,即使之后应用。
我不明白为什么,但我测试了它,就是这样。
你可以设置你的
msgBox
样式表,对于按钮,你可以直接改变它的样式表如下:
msgBox->setStyleSheet( "background-color: green;" );
msgBox->button(QMessageBox::Ok)->setStyleSheet( "background-color: white; color: red;" );