Qt4 style sheet 自定义属性选择器selector

 

Qt4 style sheet 自定义属性选择器selector

by  virusfun

现在项目的GUI里style设置基本都通过stylesheet来做的,里面有几个比较好用的记下来。

在Qt帮助文档里,stylesheet的语法里有通过selector来设置style的方法,其中有两个有意思的是:




Property SelectorQPushButton[flat="false"]Matches instances of QPushButton  that are not flat . You may use this selector to test for any Qt property  that supports QVariant::toString () (see the toString() function documentation for details). In addition, the special class  property is supported, for the name of the class.

This selector may also be used to test dynamic properties. For more information on customization using dynamic properties, refer to Customizing Using Dynamic Properties .

Instead of = , you can also use ~=  to test whether a Qt property of type QStringList  contains a given QString .

Warning:  If the value of the Qt property changes after the style sheet has been set, it might be necessary to force a style sheet recomputation. One way to achieve this is to unset the style sheet and set it again.

ID  SelectorQPushButton#okButtonMatches all QPushButton  instances whose object name  is okButton .

第二个是通过object name来判断

比如QTMyWidget里定义了一个 QLabel* m_pLabelName;

给它设置一个objectname: m_pLabelName->setObjectName("Name");

然后就可以通过在CSS文档里添加下面的代码来set style

QTMyWidget QLabel#Name   {       background: none;       color: white;   }  
QTMyWidget QLabel#Name { background: none; color: white; }  

回头重点强调第一个通过Property Selector来set style, 这一点可以跟自定义Property结合起来,功能强大。

QWidget已经有了很多默认的Property,我们还可以通过Qt宏Q_PROPERTY给widget添加自定义的Property,

Q_PROPERTY宏的定义:

Q_PROPERTY(type name


READ getFunction


[WRITE setFunction]


[RESET resetFunction]


[DESIGNABLE bool]


[SCRIPTABLE bool]


[STORED bool]


[USER bool])

使用比如

class QTMyWidget : public QFrame   {       Q_OBJECT       Q_PROPERTY(bool active READ active WRITE setActive)   ...   public:       bool active()const;       void setActive(bool active);   private:       bool    m_bActive;   }  
QTMyWidget[active="true"] { border-width: 4px 4px 4px 4px; }

这样就能设置QTMyWidget在active == true时的style了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值