qss中使用自定义属性(property)

来源:互联网 发布:电脑桌面整理软件 编辑:程序博客网 时间:2024/05/17 01:30
<span class="hljs-tag" style="box-sizing: border-box; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></span><p style="font-family: Arial; font-size: 14px; color: rgb(54, 46, 43); line-height: 26px;"><span style="word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋体, Arial; line-height: 26px; font-size: 14px;"></span><span style="margin: 0px; padding: 0px; border: none; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">Q_PROPERTY(type name READ getFunction [WRITE setFunction] [RESET resetFunction] [NOTIFY notifySignal] [DESIGNABLE </span><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">bool</span><span style="margin: 0px; padding: 0px; border: none; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">] [SCRIPTABLE </span><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">bool</span><span style="margin: 0px; padding: 0px; border: none; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">] [STORED </span><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">bool</span><span style="margin: 0px; padding: 0px; border: none; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">] [USER </span><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">bool</span><span style="margin: 0px; padding: 0px; border: none; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">] [CONSTANT] [FINAL])</span><span style="word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋体, Arial; line-height: 26px; font-size: 14px;"></span></p><p style="color: rgb(54, 46, 43); line-height: 26px;"><span style="font-family:SimHei;font-size:12px;">1.必须有一个<span style="color: rgb(102, 102, 102); line-height: 26px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">READ </span>函数。它用来读取属性值。因此用const限定。它的返回值类型必须为属性类型或者属性类型的引用或者指针。不能是其他类型例如:<span style="color: rgb(54, 53, 52); line-height: 15px; background-color: rgb(230, 231, 232);"><a target=_blank href="http://doc.qt.nokia.com/latest/qwidget.html#focus-prop" style="color: rgb(0, 115, 47); text-decoration: none;">QWidget::hasFocus</a>(). </span> 2.有一个可选的<span style="color: rgb(102, 102, 102); line-height: 26px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">WRITE </span>函数。它用来设置属性值,它的返回值必须为void型,而起必须要含有一个参数。例如:<span style="color: rgb(54, 53, 52); line-height: 15px; background-color: rgb(230, 231, 232);"><a target=_blank href="http://doc.qt.nokia.com/latest/qwidget.html#enabled-prop" style="color: rgb(0, 115, 47); text-decoration: none;">QWidget::setEnabled</a>()</span>3.一个<span style="color: rgb(102, 102, 102); line-height: 26px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">RESET </span>函数能够把property设置成其默认状态,它也是可选的。<span style="color: rgb(54, 53, 52); line-height: 15px; background-color: rgb(230, 231, 232);">复位功能必须返回void,并且不带参数,</span></span></p><p style="color: rgb(54, 46, 43); line-height: 26px;"></p><p style="color: rgb(54, 46, 43); line-height: 26px;"><span style="color: rgb(54, 53, 52); line-height: 15px; background-color: rgb(230, 231, 232);"><span style="font-family:SimHei;font-size:12px;">4.一个<tt style="word-spacing: 5px;">NOTIFY</tt>信号是可选的 。如果定义,它提供了一个信号这个信号在值发生改变时会自动被触发。</span></span></p><p style="color: rgb(54, 46, 43); line-height: 26px;"><span style="font-family:SimHei;font-size:12px;">5.如果定义了"STODE"属性表明这是一直存在的。<a target=_blank href="http://blog.csdn.net/hufeng825/article/details/qwidget.html#minimumSize-prop" style="color: rgb(0, 115, 47); text-decoration: none;">QWidget::minimumSize</a>()6.一个"DESIGNABLE"属性表明该property能在GUI builder(一般为Qt Designer)可见</span></p><p style="color: rgb(54, 46, 43); line-height: 26px;"><span style="font-family:SimHei;font-size:12px;"><span style="color: rgb(54, 53, 52); line-height: 15px; background-color: rgb(230, 231, 232);"><tt style="word-spacing: 5px;">7.USER</tt> 属性 表面是否可以被用户所编辑</span></span></p><p style="color: rgb(54, 46, 43); line-height: 26px;"><span style="color: rgb(54, 53, 52); line-height: 15px; background-color: rgb(230, 231, 232);"><span style="font-family:SimHei;font-size:12px;">8 CONST设定属性是不可修改的 所以不能跟WRITE或者NOTIFY同时出现</span></span></p><p style="color: rgb(54, 46, 43); line-height: 26px;"><span style="font-family:SimHei;font-size:12px;"><span style="color: rgb(54, 53, 52); line-height: 15px; background-color: rgb(230, 231, 232);">9</span><span style="color: rgb(54, 53, 52); line-height: 15px; background-color: rgb(230, 231, 232);">FINAL表明该属性不会被派生类中重写</span></span></p><span style="font-family: 'Source Code Pro', monospace; font-size: 14px;"></span>
<span style="font-family:Source Code Pro, monospace;"><span style="font-size: 14px; line-height: 20px;">模版:</span></span>
<span class="hljs-tag" style="box-sizing: border-box; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="font-family: 'Source Code Pro', monospace; font-size: 14px;"><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="white-space:pre"></span>Q_OBJECT    </span></span></span>
<span class="hljs-tag" style="box-sizing: border-box; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="font-family: 'Source Code Pro', monospace; font-size: 14px;"><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="white-space:pre"></span>Q_PROPERTY(QColor normalColor READ normalColor WRITE setNormalColor DESIGNABLE true)    </span></span></span>
<span class="hljs-tag" style="box-sizing: border-box; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="font-family: 'Source Code Pro', monospace; font-size: 14px;"><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">public:    </span></span></span>
<span class="hljs-tag" style="box-sizing: border-box; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="font-family: 'Source Code Pro', monospace; font-size: 14px;"><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="white-space:pre"></span>explicit StyledWidget(QWidget *parent = </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">0</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">);    </span></span></span>
<span class="hljs-tag" style="box-sizing: border-box; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="font-family: 'Source Code Pro', monospace; font-size: 14px;"><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="white-space:pre"></span>~StyledWidget();    </span></span></span>
<span class="hljs-tag" style="box-sizing: border-box; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="font-family: 'Source Code Pro', monospace; font-size: 14px;"><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="white-space:pre"></span>QColor normalColor() const;    </span></span></span>
<span class="hljs-tag" style="box-sizing: border-box; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="font-family: 'Source Code Pro', monospace; font-size: 14px;"><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="white-space:pre"></span>void setNormalColor(QColor color);</span></span></span>
<span class="hljs-tag" style="box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 14px; line-height: 35px;">QSS中使用自定义的属性设置对应的样式:</span></span>
<span class="hljs-tag" style="box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">StyledWidget</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-rules" style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">{        </span>
<span class="hljs-rules" style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;"><span style="white-space:pre"></span>qproperty-normalColor</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"> white</span></span>;        </span>
<span class="hljs-rules" style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;"><span style="white-space:pre"></span>qproperty-disableColor</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"> gray</span></span>;        </span>
<span class="hljs-rules" style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;"><span style="white-space:pre"></span>qproperty-highlightColor</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-function" style="box-sizing: border-box;">rgb(<span class="hljs-number" style="box-sizing: border-box;">0</span>, <span class="hljs-number" style="box-sizing: border-box;">160</span>, <span class="hljs-number" style="box-sizing: border-box;">230</span>)</span></span></span>;        </span>
<span class="hljs-rules" style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;"><span style="white-space:pre"></span>qproperty-errorColor</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"> red</span></span>;</span>
<span class="hljs-rules" style="box-sizing: border-box; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588); line-height: 20px; font-size: 14px; font-family: 'Source Code Pro', monospace; color: rgb(51, 51, 51);"><span class="hljs-rule" style="box-sizing: border-box;">}</span></span>
<span class="hljs-rules" style="box-sizing: border-box; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588); line-height: 20px; font-size: 14px; font-family: 'Source Code Pro', monospace; color: rgb(51, 51, 51);"><span class="hljs-rule" style="box-sizing: border-box;"></span></span>
<span class="hljs-rules" style="box-sizing: border-box; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588); line-height: 20px; font-size: 14px; font-family: 'Source Code Pro', monospace; color: rgb(51, 51, 51);"><span class="hljs-rule" style="box-sizing: border-box;">目前get,set测试通过。但是,当自定义属性的控件的qss修改之后,自定义属性需要自动还原为默认值时,没有调用RESET函数。哪位知道请赐教。</span></span>
0 1
原创粉丝点击