Qt样式表语法不起作用?

Moj*_*adi 4 qt stylesheet qtstylesheets

我正在使用Qt 5.0.1.我想使用样式表语法来自定义小部件及其元素的功能.

但有些语法如QLineEdit { color: red }或

QPushButton#evilButton:pressed {
     background-color: rgb(224, 0, 0);
     border-style: inset;
 }
Run Code Online (Sandbox Code Playgroud)

不工作,编译器给出错误.

我改变了一些语法并得到了答案.例如:

QPushButton#evilButton {
 background-color: red;
 border-style: outset;
 border-width: 2px;
 border-radius: 10px;
 border-color: beige;
 font: bold 14px;
 min-width: 10em;
 padding: 6px;
Run Code Online (Sandbox Code Playgroud)

}

转换成:

mypushbutton->setStyleSheet("background-color:purple;"
                     "border-style:inset;"
                     "border-width: 4px;"
                     "border-color: green;"
                     "border-radius: 10px;"
                     "font: bold 14px;"
                     "min-width: 10em;"
                     "padding: 6px;"
                     );
Run Code Online (Sandbox Code Playgroud)

事实上,我使用了另一个功能来做到这一点.但是我无法改变第一个代码而不知道该怎么做......我该怎么办?我应该做些include什么或其他问题吗?我用这个页面来学习样式表语法.即使我自己的Qt的例子不起作用,只是错误引起... !!! ???

小智 7

尝试了你的例子,它适用于Qt5

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setStyleSheet(
                "QPushButton#evilButton {"
                "background-color: red;"
                "border-style: outset;"
                "border-width: 2px;"
                "border-radius: 10px;"
                "border-color: beige;"
                "font: bold 14px;"
                "min-width: 10em;"
                "padding: 6px; }"
                );
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我将qss存储在文件中并从中加载样式