Qt Style Sheets

本文深入探讨Qt样式表的使用方法,包括级联设置、选择器类型、伪状态、冲突解析及级联规则。通过实例说明如何自定义界面样式,适用于Qt界面设计人员。

Qt设置界面方式

1.继承QStyle,实现自定义子类

 

2.Qt Style Sheets(类似与HTML CSS)配置

概览

       QApplication::setStyleSheet设置应用程序的全部样式;

       QWidget::setStyleSheet设置特定QWidget或者其子类的样式。

       Qt Style Sheets支持级联设置,比QPalette的功能更加强大,当一个style sheet设置后,QWidget::style()将返回样式包装器,而不是平台本身的样式。

语法

       1selector+declaration  selector是界面 declaration是界面属性

例如:QPushButton {color:red}  QPushButton是selector {color:red}是declaration

规则:QPushButton和它的子类的前景色是红色。

        2.declaration大小写不敏感,color Color COLOR和cOloR表示同一属性。

        3.selector带下斜敏感,类名  对象名和Qt属性名(?)

        4.不同的selector可以用逗号(,)分隔组装后,使用相同的declaration。

例如:QPushButton,QLineEdit,QComboBox{color:red} == 

QPushButton{color:red}

QLineEdit{color:red}       

QComboBox{color:red}

        5.declaration由一组property:value组成,用{}包起来。

例如:QPushButton{color:red; background-color:white}

         6.selector类型

universal selector*所有widgets
type selectorQPushButtonQPushButton和它的子类
property selectorQPushButton[flat="false"]

flat是false的QPushButton

= QWidget中的属性必须支持可以调用toString,

~=QWidget中的属性包含设置的属性名

class selector.QPushButtonQPushButton,不包含其子类,该表达式等价于 *[class ~= "QPushButton"]
id selectorQPushButton#okButtonQPushButton对象名为okButton, object name
descendant selectorQDialog QPushButtonQPushButton的父对象、祖父对象等是QDialog
child selectorQDialog QPushButtonQPushButton的父对象是QDialog(直属对象)

        7.sub-controls

        复杂界面,有子控制对象的widget,例如QComboBox包含drop-down按钮,QSpinBox包含up and down arrows。

selector可以包含子控制对象。例如

       QComboBox::drop-down{image:url(dropdown.png)}

        8.pseudo-states

       selectors可以包含pseudo-states,设置widget某一种状态。用冒号(:)间隔。例如 QPushButton:hover{color:white}

支持!符号 如:QRadioButton:!hover{color:red}

支持串联,如:QCheckBox:hover:checked{color:white}     

                          QPushButton:hover:!pressed{color:blue}

                          QCheckBox:hover, QCheckBox:checked{color:white}

                           QComboBox::drop-down:hover{image:url(dropdown_bright.png)}

         9conflic Resolution

         考虑到对同一对象的同一属性多次设置样式,如

         a. QPushButton#okButton{color:gray}

         b. QPushButton{color:red}

          a样式比b样式更具针对性,故优先。

         带有pseudo-states的selector,如

         a. QPushButton:hover{color:white}

         b. QPushButton{color:red}

         a比b具有高优先级

       

          对于冲突的属性设置,遵循下列规则

          1.

 

           10.级联

           qApp->setStyleSheet("QPushButton{color:white}");

           myPushButton->setStyleSheet("color:blue");

           myPushButton的color为blue.

           11.继承

           qApp->setStyleSheet("QGroupBox{color:red;}");

如果QPushButton是QGroupBox的子对象,QPushButton不继承QGroupBox的设置,若需要QPushButton继承样式,

qApp->setStyleSheet("QGroupBox, QGroupBox *{color:red;}");

或者qApp->setAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles, true);

如果palette和style sheet同时设置,style sheet具有高优先级。

            12.命名空间

             namespace ns{

            class MyPushButton : public QPushButton{};

            }

            qApp->setStyleSheet("ns--MyPushButton{background-color:yellow;}");

           

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值