赞
踩
Qt样式表分离中需要使用到Q_PROPERTY宏,在代码现在的版本中,使用样式表主要还是使用setStyleSheet(),但是考虑到后续样式表分离,使用QSS就不得不提及Q_PROPERTY了。
Qt Style Sheets Examples:
https://doc.qt.io/archives/qt-4.8/stylesheet-examples.html
Q_PROPERTY:
https://blog.csdn.net/liang19890820/article/details/51698536
https://doc.qt.io/archives/qt-4.8/qobject.html#Q_PROPERTY
https://doc.qt.io/archives/qt-4.8/properties.html#qt-s-property-system
Q_PROPERTY(type name
READ getFunction
[WRITE setFunction]
[RESET resetFunction]
[NOTIFY notifySignal]
[DESIGNABLE bool]
[SCRIPTABLE bool]
[STORED bool]
[USER bool]
[CONSTANT]
[FINAL])
其中READ getFunction必须要实现,其他例如write/reset等函数随意,看具体需要的功能。一般会实现read,write和reset。
/// 官方例程 class MyClass : public QObject { Q_OBJECT Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged) Q_ENUMS(Priority) public: MyClass(QObject *parent = 0); ~MyClass(); enum Priority { High, Low, VeryHigh, VeryLow }; void setPriority(Priority priority) { m_priority = priority; emit priorityChanged(priority); } Priority priority() const { return m_priority; } signals: void priorityChanged(Priority); private: Priority m_priority; }; ...... MyClass *myinstance = new MyClass; QObject *object = myinstance; myinstance->setPriority(MyClass::VeryHigh); object->setProperty("priority", "VeryHigh");/*动态设置属性*/
手工更新例程:
m_pMaximizeButton->setProperty("maximizeProperty", bMaximize ? "restore" : "maximize");
m_pMaximizeButton->style()->unpolish(m_pMaximizeButton);
m_pMaximizeButton->style()->polish(m_pMaximizeButton);
m_pMaximizeButton->update();
小demo:
// qsshelper.h/cpp QString qsshelper::qssopen(const QString& filepath) /// 打开qss文件 { QFile qssfile(filepath); if(qssfile.exists()&&qssfile.open(QFile::ReadOnly)) { str = QString(qssfile.readAll()); qssfile.close(); } return str; } // qsstest.h/cpp /**更新按键的字体大小**/ void QThreadTest::updatePtnStyle() { DssPBtn1->setProperty("font-size","20px"); DssPBtn2->setProperty("font-size", "20px"); DssPBtn1->style()->unpolish(DssPBtn1); DssPBtn1->style()->polish(DssPBtn1); DssPBtn2->style()->unpolish(DssPBtn2); DssPBtn2->style()->polish(DssPBtn2); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。