4

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

但有些语法像QLineEdit { color: red }or

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 示例也不起作用,只会引发错误......!!!???

4

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

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

于 2013-06-01T21:17:16.197 回答