Qt QSS 使用

1.在资源文件建立一个qss文件。如blue.qss

2. 调用

 

#include "mainwindow.h"
#include <QApplication>
#include<QFile>
#include "mainframe.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QFile qss_file(":/res/blue.qss");
    qss_file.open(QFile::ReadOnly);
    qApp->setStyleSheet(qss_file.readAll());
    qss_file.close();
    //MainWindow w;
    //w.show();
    MainFrame w;
    w.show();
    return a.exec();
}

 如果控件是代码建立的,需要使用id选择器方法用setObjectName指定id 名字,如下:

    QLabel *lblWindowTitle = new QLabel(this);
    //lblWindowTitle->setStyleSheet("color: rgb(255, 255, 255);font: 75 12pt \"Verdana\";");
    lblWindowTitle->setObjectName("lblWindowTitle");
QLabel#lblWindowTitle{
      background:transparent;
      border-style:none;
      color: rgb(255, 255, 255);
      font: 75 12pt "Verdana";
}

 

 

QPushButton#btnNavRealImage,QPushButton#btnNavScan,QPushButton#btnNavBrowse{
    width:70px;
    height:40px;
    text-align: center;
    color:white;
    font-style:normal;
    border:5px;
}
QPushButton#btnNavRealImage:hover,QPushButton#btnNavScan:hover,QPushButton#btnNavBrowse:hover{
   background-color:#549cd8;
}

 

 

3.

 

一、QSS语法
同css一样,他也有由一个selector与一个declaration组成,selector指定了是对哪一个控件产生效果,而declaration才是真正的产生作用的语句。如: QPushButton { color: red } QPushButton指定了是对所有的QPushButton或是其子类控件(如用户定义的MyPushButton)产生影响,而color:red表明所有的受影响控件的前景色都为red。 除了“类名”,“对象名”,“Qt属性名”这三样东西是大小写敏感的外其他的东西都是大小写不敏感的,如color与Color代表同一属性。 如果有几个selector指定了相同的declaration, 可以使用逗号(,)将各个选择器分开,如: QPushButton, QLineEdit, QComboBox { color: red } 他相当于: QPushButton { color: red } QLineEdit { color: red } QComboBox { color: red } declaration部份是一系列的(属性:值)对,使用分号(;)将各个不同的属性值对分开,使用大括号({})将所有declaration包含在一起。
1、 一般选择器(selector)
Qt支持所有的CSS2定义的选择器,其祥细内容可以在w3c的网站上查找http://www.w3.org/TR/CSS2/selector.html , 其中比较常用的selector类型有:

  • 通用类型选择器:* 会对所有控件有效果。
  • 类型选择器:QPushButton匹配所有QPushButton的实例和其子类的实例。
  • 属性选择器:QPushButton[flat=”false”]匹配所有QPushButton属性flat为false的实例,属性分为两种,静态的和动态的,静态属性可以通过Q_PROPERTY() 来指定,来动态属性可以使用setProperty来指定,如: QLineEdit *nameEdit = new QLineEdit(this); nameEdit->setProperty("mandatoryField", true); 如果在设置了qss后Qt属性改变了,需要重新设置qss来使其生效,可以使用先unset再set qss。
  • 类选择器:.QPushButton匹配所有QPushButton的实例,但不包含其子类,这相当于:*[class~="QPushButton"]     ~=的意思是测试一个QStringList类型的属性是否包含给定的QString
  • ID选择器:QPushButton#okButton对应Qt里面的object name设置,使用这条CSS之前要先设置对应控件的object name为okButton,如:Ok->setObjectName(tr(“okButton”));
  • 后裔选择器:QDialog QPushButton匹配所有为QDialog后裔(包含儿子,与儿子的儿子的递归)为QPushButton的实例
  • 子选择器:QDialog > QPushButton匹配所有的QDialog直接子类QPushButton的实例,不包含儿子的儿子的递归。

2、子控件选择器

  • 对于复杂的控件,可能会在其中包含其他子控件,如一个QComboxBox中有一个drop-down的按钮。那么现在如果要设置QComboxBox的下拉按钮的话,就可以这样访问: QComboBox::drop-down { image: url(dropdown.png) }其标志是(::)
  • 子控件选择器是用位置的引用来代表一个元素,这个元素可以是一个单一控件或是另一个包含子控件的复合控件。使用subcontrol-origin属性可以改变子控件的默认放置位置,如: QComboBox {        margin-right: 20px; } QComboBox::drop-down {        subcontrol-origin: margin; }
    如上语句可以用来改变drop-down的margin。
  • 相对位置属性可以用来改变子控件相对于最初位置的偏移量,如当一个QCombox的drop-down按钮被按下时,我们可以用一个内部的小偏移量来表示被按下的效果,如下: QComboBox::down-arrow {        image: url(down_arrow.png); } QComboBox::down-arrow:pressed {        position: relative;        top: 1px; left: 1px; }
  • 绝对位置属性允许子控件改变自己的位置和大小而不受引用元素的控件。一但位置被设定了,这些子控件就可以被当成一般的widget来对待,并且可以使用合模型。关于合模型可以参考下面。

            如果你要查看Qt支持哪些子控件选择器,可以参考:http://pepper.troll.no/s60prereleases/doc/stylesheet-reference.html#list-of-sub-controls-syntax.html

3、伪选择器(pseudo-states)

  • 伪选择器以冒号(:)表示,与css里的伪选择器相似,是基于控件的一些基本状态来限定程序的规则,如hover规则表示鼠标经过控件时的状态,而press表示按下按钮时的状态。如: QPushButton:hover {        } 表示鼠标经过时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使用伪选择器,如hover,press,enabled等,如:按扭“1”是disable了的,QPushButton:!enabled {color: white}
    • 在程序里面要先设置控件的,如: 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
  • 所有的类型选择器都有一个共同的特性,就是如果有两个属性冲突了的话就会以最后出现的一个为准,如: 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。

三、级联效应

  • 子类可以继承父类的StyleSheet,但是如果子类里面设置了StyleSheet与父类里在设置的有冲突,那么当然会优先考虑子类自己的, 同样,如果在qApp时面设置了,但是在某一个特定控件里面也设置,如果有冲突,也是优先控件自己的,例如,我在程序时面设置了:btnOneEn->setStyleSheet("QPushButton { color: red }"); 而,当我再设置qApp时,如果,将QPushButton的color设置成grey的,那么结果是对于btnOneEn这个QPushButton来说他的颜色还是red。 这就是为什么这里设置为grey了btnOneEn却还是red的。
  • 如果我们对一个控件设置StyleSheet为: QPushButton* myPushButton; myPushButton->setStyleSheet("* { color: blue }"); 其实他和设置为:myPushButton->setStyleSheet("color: blue"); 效果相同,只是后一种设置不会对QPushButton的子类产生作用,但第一种却会。

四、继承性
与CSS不同的一点,在CSS box模型中,如果一个元素在别一元素的里面,那么里面的元素会自动继承外面元素的属性,但QSS里面不会,如: 一个QPushButton如果放在一个QGroupBox里面,如果:qApp->setStyleSheet("QGroupBox { color: red; } "); 并不代表在QGroupBox里面的QPushButton也会有color:red的属性,如果要想有的话要显示写明,如:qApp->setStyleSheet("QGroupBox, QGroupBox * { color: red; }"); 或者在应用程序里面也可以用QWidget::setFont等来设置到子控件的属性。
五、Namespace冲突
类型选择器能够使用到一个特定的类型,如: class MyPushButton : public QPushButton {       // ... } qApp->setStyleSheet("MyPushButton { background: yellow; }");
因为QSS使用QObject::className来判断要赋与style sheet的控件类型,如果一个用户定义控件类型在一个namespace里面的话,QObject::className会返回<namespace>::<classname> 的名字,这和子控件选择器的语法相冲突,为了解决此问题,使用“--”来代替“::”,比如: namespace ns {       class MyPushButton : public QPushButton {           // ...       } } qApp->setSytleSheet("ns--MyPushButton { background: yellow; }");
六、设置对像属性 如果在程序里面使用Q_PROPERTY设置的属性,可以在qss里面使用:qproperty-<property name>的形式来访问并设置值。如: MyLabel { qproperty-pixmap: url(pixmap.png); } MyGroupBox { qproperty-titleColor: rgb(100, 200, 100); } QPushButton { qproperty-iconSize: 20px 20px; } 如果属性引用到的是一个由Q_ENUMS申明的enum 时,要引用其属性名字要用定义的名称而不是数字。

 

3.下面是一个蓝色主题的css文件例子
 

 

QWidget#frmLogin,QWidget#frmMessageBox,QWidget#Dialog3,QWidget#Dialog1{
        border:1px solid #1B89CA;
        border-radius:2px;
}

.QFrame{
        border:1px solid #5CACEE;
        border-radius:5px;
}

QWidget#widget_title{
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #1B89CA, stop:1 #1077B5);
}

QLabel#lab_Ico,QLabel#lab_Title{
        padding-left:3px;

        border-radius:0px;
        color: #F0F0F0;
        background-color:rgba(0,0,0,0);
        border-style:none;
}

QLineEdit {
        border: 1px solid #5CACEE;
        border-radius: 5px;
        padding: 2px;
        background: none;
        selection-background-color: #1B89CA;
}

QLineEdit[echoMode="2"] {
        lineedit-password-character: 9679;
}

.QGroupBox{
        border: 1px solid #5CACEE;
        border-radius: 5px;
}

.QPushButton{
        border-style: none;
        border: 0px;
        color: #F0F0F0;
        padding: 5px;
        min-height: 20px;
        border-radius:5px;
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #1B89CA, stop:1 #1077B5);
}

.QPushButton[focusPolicy="0"] {
        border-style: none;
        border: 0px;
        color: #F0F0F0;
        padding: 0px;
        min-height: 10px;
        border-radius:3px;
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #1B89CA, stop:1 #1077B5);
}

.QPushButton:hover{
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #5CACEE, stop:1 #4F94CD);
}

.QPushButton:pressed{
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #1B89CA, stop:1 #1077B5);
}

QPushButton#btnMenu,QPushButton#btnMenu_Min,QPushButton#btnMenu_Max,QPushButton#btnMenu_Close{
        border-radius:0px;
        color: #F0F0F0;
        background-color:rgba(0,0,0,0);
        border-style:none;
}

QPushButton#btnMenu:hover,QPushButton#btnMenu_Min:hover,QPushButton#btnMenu_Max:hover{
        background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 rgba(25, 134, 199, 0), stop:1 #5CACEE);
}

QPushButton#btnMenu_Close:hover{
        background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 rgba(238, 0, 0, 128), stop:1 rgba(238, 44, 44, 255));
}

QCheckBox {
        spacing: 2px;
}

QCheckBox::indicator {
        width: 20px;
        height: 20px;
}

QCheckBox::indicator:unchecked {
        image: url(:/image/checkbox_unchecked.png);
}

QCheckBox::indicator:checked {
        image: url(:/image/checkbox_checked.png);
}

QRadioButton {
        spacing: 2px;
}

QRadioButton::indicator {
        width: 15px;
        height: 15px;
}

QRadioButton::indicator::unchecked {
        image: url(:/image/radio_normal.png);
}

QRadioButton::indicator::checked {
        image: url(:/image/radio_selected.png);
}

QComboBox,QDateEdit{
        border-radius: 3px;
        padding: 1px 10px 1px 5px;
        border: 1px solid #5CACEE;
}

QComboBox::drop-down,QDateEdit::drop-down {
        subcontrol-origin: padding;
        subcontrol-position: top right;
        width: 15px;
        border-left-width: 1px;
        border-left-style: solid;
        border-top-right-radius: 3px;
        border-bottom-right-radius: 3px;
        border-left-color: #5CACEE;
}

QComboBox::down-arrow,QDateEdit::down-arrow {
        image: url(:/image/array_down.png);
}

QMenu {
        background-color:#F0F0F0;
        margin: 2px;
}

QMenu::item {
        padding: 2px 12px 2px 12px;
}

QMenu::indicator {
        width: 13px;
        height: 13px;
}

QMenu::item:selected {
        color: #FFFFFF;
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #1B89CA, stop:1 #1077B5);
}

QMenu::separator {
        height: 1px;
        background: #5CACEE;
}

QProgressBar {
        border-radius: 5px;
        text-align: center;
        border: 1px solid #5CACEE;
}

QProgressBar::chunk {
        width: 5px;
        margin: 0.5px;
        background-color: #1B89CA;
}

QSlider::groove:horizontal,QSlider::add-page:horizontal {
        background: #808080;
        height: 8px;
        border-radius: 3px;
}

QSlider::sub-page:horizontal {
        height: 8px;
        border-radius: 3px;
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #1B89CA, stop:1 #1077B5);
}

QSlider::handle:horizontal {
        width: 13px;
        margin-top: -3px;
        margin-bottom: -3px;
        border-radius: 6px;
        background: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5,stop:0.6 #F0F0F0, stop:0.778409 #5CACEE);
}

QSlider::handle:horizontal:hover {
        background: qradialgradient(spread: pad, cx: 0.5, cy: 0.5, radius: 0.5, fx: 0.5, fy: 0.5, stop: 0.6 #F0F0F0,stop:0.778409 #1B89CA);
}

QSlider::groove:vertical,QSlider::sub-page:vertical {
        background:#808080;
        width: 8px;
        border-radius: 3px;
}

QSlider::add-page:vertical {
        width: 8px;
        border-radius: 3px;
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #1B89CA, stop:1 #1077B5);
}

QSlider::handle:vertical {
        height: 14px;
        margin-left: -3px;
        margin-right: -3px;
        border-radius: 6px;
        background: qradialgradient(spread: pad, cx: 0.5, cy: 0.5, radius: 0.5, fx: 0.5, fy: 0.5, stop: 0.6 #F0F0F0, stop:0.778409 #5CACEE);
}

QSlider::handle:vertical:hover {
        background: qradialgradient(spread: pad, cx: 0.5, cy: 0.5, radius: 0.5, fx: 0.5, fy: 0.5, stop: 0.6 #F0F0F0,stop:0.778409 #1B89CA);
}

QScrollBar:vertical {
        width:10px;
        background-color:rgba(0,0,0,0%);
        padding-top:10px;
        padding-bottom:10px;
}

QScrollBar:horizontal {
        height:10px;
        background-color:rgba(0,0,0,0%);
        padding-left:10px; padding-right:10px;
}

QScrollBar::handle:vertical {
        width:10px;
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #5CACEE, stop:1 #4F94CD);
}

QScrollBar::handle:horizontal {
        height:10px;
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #5CACEE, stop:1 #4F94CD);
}

QScrollBar::handle:vertical:hover {
        width:10px;
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #1B89CA, stop:1 #1077B5);
}

QScrollBar::handle:horizontal:hover {
        height:10px;
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #1B89CA, stop:1 #1077B5);
}

QScrollBar::add-line:vertical {
        height:10px;
        width:10px;
        subcontrol-position: bottom;
        subcontrol-origin: margin;
        border-image:url(:/image/add-line_vertical.png);
}

QScrollBar::add-line:horizontal {
        height:10px;
        width:10px;
        subcontrol-position: right;
        subcontrol-origin: margin;
        border-image:url(:/image/add-line_horizontal.png);
}

QScrollBar::sub-line:vertical {
        height:10px;
        width:10px;
        subcontrol-position: top;
        subcontrol-origin: margin;
        border-image:url(:/image/sub-line_vertical.png);
}

QScrollBar::sub-line:horizontal {
        height:10px;
        width:10px;
        subcontrol-position: left;
        subcontrol-origin: margin;
        border-image:url(:/image/sub-line_horizontal.png);
}

QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical {
        width:10px;
        background: #C0C0C0;
}

QScrollBar::add-page:horizontal,QScrollBar::sub-page:horizontal {
        height:10px;
        background: #C0C0C0;
}

QScrollArea {
        border: 0px ;
}

QTreeView,QListView,QTableView{
        border: 1px solid #5CACEE;
        selection-background-color: #1B89CA;
        selection-color: #F0F0F0;
}

QTableView::item:selected, QListView::item:selected, QTreeView::item:selected {
        color: #F0F0F0;
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #1B89CA, stop:1 #1077B5);
}

QTableView::item:hover, QListView::item:hover, QTreeView::item:hover {
        color: #F0F0F0;
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #5CACEE, stop:1 #4F94CD);
}

QTableView::item, QListView::item, QTreeView::item {
        padding: 5px;
        margin: 0px;
}

QHeaderView::section {
        padding:3px;
        margin:0px;
        color:#F0F0F0;
        border: 1px solid #F0F0F0;
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #5CACEE, stop:1 #4F94CD);
}

QTabBar::tab {
        border-bottom-left-radius:0px;
        border-bottom-right-radius:0px;
        color: #F0F0F0;
        min-width: 60px;
        min-height: 20px;
        padding: 3px 8px 3px 8px;
        margin:1px;
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #5CACEE, stop:1 #4F94CD);
}

QTabBar::tab:selected, QTabBar::tab:hover {
        background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #1B89CA, stop:1 #1077B5);
}

QStatusBar::item {
     border: 1px solid #5CACEE;
     border-radius: 3px;
}

 

posted @ 2015-12-09 10:46  ike_li  阅读(1538)  评论(0编辑  收藏  举报