Qt样式表语法不起作用?

4
我正在使用Qt 5.0.1。我想使用样式表语法来自定义小部件及其元素的特性。但是一些语法,如QLineEdit { color: red }或...
QPushButton#evilButton:pressed {
     background-color: rgb(224, 0, 0);
     border-style: inset;
 }

不能工作,编译器报错。

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

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;

转换为:

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;"
                     );

事实上,我用了另一个函数来做这件事。但是我无法更改第一段代码,也不知道该怎么办...我应该怎么办?我需要include某些内容或者其他问题?我使用了这个页面来学习样式表语法。即使是我的Qt示例也无法正常工作,出现错误...!!!???


1
没有人能回答我的问题吗?这太重要了...请帮忙。 - Mojtaba Ahmadi
1个回答

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; }"
                );
}

顺便说一下,我将样式表存储在文件中,并从中加载样式。

1
忘了说。您可以为整个应用程序设置QSS。qApp->setStyleSheet("<您的QSS>"); - Ruslan Salikhov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接