Qt样式表错误?

时间:2011-08-23 19:23:21

标签: qt user-interface styles stylesheet

我在简单的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();
}

qt 4.7.3-3; arch linux; gnome 3后备模式

4 个答案:

答案 0 :(得分:8)

对于第一个复选框,问题出现在Gtk +样式中: enter image description here

QLabelQPushButtonQCheckBox不支持text-decoration伪状态css选择器中的字体更改(包括:hover)。最简单的小部件似乎是QToolButton(如果你确实需要,它可以用来替换QLabel)。
所有小部件都支持该状态的调色板更改(前景色和背景色和样式)。

答案 1 :(得分:4)

我不能说你的复选框有什么问题,但是根据Qt的样式表参考可以找到以下的花絮(我在网上找不到,但它在Qt助手中)

  

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

换句话说,QLabel:不支持悬停。

答案 2 :(得分:2)

而不是

// some comment

使用

/* some comment */

。我猜他们选择后一种形式,因为它不受新线断裂的影响。

除此之外,如果仍然不行,那么你似乎遇到了错误:https://bugreports.qt-project.org//browse/QTBUG-4307

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

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

对我有用。

答案 3 :(得分:0)

不应该

color: red

color: red;

在发布的代码中缺少分号。