您的位置:首页 > 编程语言 > Qt开发

详解 QT 皮肤 QSS编程(2)

2012-12-18 09:10 417 查看
3、伪选择器(pseudo-states)

伪选择器以冒号(:)表示,与css里的伪选择器相似,是基于控件的一些基本状态来限定程序的规则,如hover规则表示鼠标经过控件时的状态,而press表示按下按钮时的状态。如:
QPushButton:hover {
Background-color:red;
}


表示鼠标经过时QPushButton背景变红。

Pseudo还支持否定符号(!),如:
QRadioButton:!hover { color: red }


表示没有鼠标移上QRadioButton时他显示的颜色是red。

Pseudo可以被串连在一起,比如:
QPushButton:hover:!pressed { color: blue; }


表示QPushButton在鼠标移上却没有点击时显示blue字,但如果点击的时候就不会显示blue颜色了。

同样可以和之前所讲的子控件选择器一起联合使用,如:
QSpinBox::down-button:hover { image: url(btn-combobox-press.bmp)}


与前面所讲的一样,伪选择器,子控件选择器等都是可以用逗号(,)分隔表示连续相同的设置的,如:

QPushButton:hover,QSpinBox::down-button, QCheckBox:checked { color: white ;image: url(btn-combobox-press.bmp);}表示如下

更多请参考:http://pepper.troll.no/s60prereleases/doc/stylesheet-reference.html#list-of-pseudo-states

二、解决冲突

使用object name

在程序里面要先设置控件的,如:
btnOne = new QPushButton(centralWidget);
btnOne->setObjectName(QString::fromUtf8("btnOneCh"));


这样,我们有了一个object name为btnOneCh的QPushButton,试验一下,使用指定object name的方式,如:
QPushButton#btnOneCh { color: red }
QPushButton { color: white }


可以看出,btnOncCh的color变成了red

使用伪选择器,如hover,press,enabled等,如:按扭“1”是disable了的,QPushButton:!enabled {color: white}

所有的类型选择器都有一个共同的特性,就是如果有两个属性冲突了的话就会以最后出现的一个为准,如:
QPushButton { color: red }
QAbstractButton { color: gray}


由于QPushButton为QAbstractButton的子类,如果只设置QAbstractButton的可以想像结果是所有的QPushButton都为gray,如果只设置QPushButton的所有QPushButton都会为red,当两个都能设置起效的时候,以在文本上最后出现的为准,所以结果为Grey

当然其中如果有#指定了object name,他所设置的优先级是最大的,具体规则可以参考:http://www.w3.org/TR/CSS2/cascade.html#specificity,

或是http://pepper.troll.no/s60prereleases/doc/stylesheet-syntax.html#conflict-resolution
QPushButton#btnOneCh { color: red }
QPushButton { color: blue }
QAbstractButton { color: gray}


虽然QAbstractButton在最后,但是之前有#btnOneCh指定了QPushButton“一”的color为red所以最后显示也是“一”为red。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: