Qt Style Sheets

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;}");

           

### 关于Qt Style Sheets Editor的许可证类型及使用限制 #### Qt Style Sheets (QSS) 的许可概述 Qt框架及其组件通常遵循两种主要类型的许可证:商业许可证和开源GNU通用公共许可证(GPL/LGPL)。具体到Qt Style Sheets Editor工具,其并未单独作为一个独立模块存在,而是集成在Qt Creator或通过其他方式提供给开发者。因此,它的许可证取决于整个Qt项目的授权模式[^1]。 如果开发人员选择基于LGPL协议下的Qt版本,则需满足以下条件才能合法使用Qt Style Sheets功能及相关编辑器: - 修改后的源码必须公开共享; - 动态链接库形式分发应用程序时允许不开放核心业务逻辑代码[^3]。 而对于采用商业版授权的情况来说,用户享有更灵活的权利去部署自己的产品而无须对外公布内部实现细节[^2]。 需要注意的是,《Style Sheet Example》案例展示的具体特性可能因不同发行版本有所差异,比如提到的Qt4.7中的演示内容或许已经更新迭代至更高版本里。对于最新稳定发布的Qt系列而言,始终建议查阅官方文档获取最权威准确的信息。 另外,在实际项目中运用诸如QLineEdit这样的控件并对其进行外观定义操作时也要注意保持一致性以及用户体验友好度等问题[^4]。 ```python # 示例:简单的QLineEdit样式设置 lineEdit.setStyleSheet(""" QLineEdit { border: 1px solid gray; border-radius: 5px; padding: 0 8px; background: white; selection-background-color: blue; } """) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值