Qt 样式表的错误?

13

我有很多简单QSS(Qt样式表)的错误。这是Qt的错误吗?

例子:

enter image description here

样式表:

#check1 {
  color: red                  //didn't work here
}

#check2 {
  color: red;                 //but work here
  background-color: black
}

#label1 {
  color: blue;
  text-decoration: underline  //work fine here
} 

#label2:hover {
  color: blue; 
  text-decoration: underline  //but didn't work here
}

来源:

#include <QtGui>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    app.setStyleSheet(" #check1 {color: red} \
                        #check2 {color: red; background-color: black}  \
                        #label1 {color: blue; text-decoration: underline}  \
                        #label2:hover {color: blue; text-decoration: underline}");
    QWidget w; w.setFixedSize(120,130);

    QCheckBox check1("checkbox1",&w);
    check1.setObjectName("check1");
    check1.move(10,0);

    QCheckBox check2("checkbox1",&w);
    check2.setObjectName("check2");
    check2.move(10,30);

    QLabel label1("label1", &w);
    label1.setObjectName("label1");
    label1.move(10,60);

    QLabel label2("label2", &w);
    label2.setObjectName("label2");
    label2.move(10,90);
    w.show();

    return app.exec();
}

在 Arch Linux 上使用 GNOME 3 回退模式时,使用的是 Qt 4.7.3-3。

4个回答

9
对于第一个复选框,问题出现在Gtk+风格中: enter image description here QLabel, QPushButtonQCheckBox不支持在:hover伪状态css选择器中更改字体(包括text-decoration)。最简单的小部件似乎是QToolButton(如果您确实需要替换QLabels,则可以使用它)。
所有小部件都支持该状态下的调色板更改(前景和背景颜色和样式)。

4
我不能确定你的复选框问题出在哪里,但是我找到了以下提示,根据Qt的样式表参考(我找不到在线版,但可以在Qt助手中找到):

QLabel支持盒子模型。不支持:hover伪状态。

换句话说,QLabel:hover不被支持。

你说得对!这很奇怪,因为根据他们的文档,它不应该这样。然而,从我的经验来看,改变下划线并不能起作用,因为我曾经尝试过在过去做和你现在做的事情一模一样。 - Chris
Qt 4.7确实支持hover。http://doc.qt.nokia.com/latest/stylesheet-syntax.html#pseudo-states - spraff
2
我并没有说Qt不支持hover伪状态,我是说它不支持QLabels的hover伪状态。 - Chris

2

替代

// some comment

使用

/* some comment */

我猜他们选择了后一种形式,因为它不受换行符破坏的影响。

除此之外,如果还不行,那么看起来你遇到了错误:https://bugreports.qt-project.org//browse/QTBUG-4307

作为仅设置文本颜色的解决方法,请尝试使用

#check1:hover { color:red; background-color:transparent; }

这对我很有效。


已添加注释,但它们并不存在于源代码中。 - TheHorse
@TheHorse:这应该在问题中提到;就目前而言,我的回答仍然适用。 - Sebastian Mach

0

不应该

color: red

color: red;

?

你发布的代码中有几个地方缺少分号。


1
最后一项后不需要分号。 - TonyK
2
...或许这是必需的。Qt正在进行解析,而不是某个浏览器。因此,Nokia有时间实现它的方式。我认为,通常假设每个人的标准中的每个模糊方便功能都已实现是一个坏习惯。如果您查看其他一些Qt类,您将始终找到未实现或以不同方式实现的内容。即使浏览器也无法跟上所有标准。 - user440297
分号没有任何变化。 - TheHorse
@user440297:我不仅是“建议”可能不需要分号。请参见http://htmlhelp.com/reference/css/structure.html。 - TonyK
Qt文档示例都省略了最后的分号 : http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html - galinette

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