achieve
1, ui defines the style sheet
// 在最开始的类中右键 - 样式表
QPushButton#button2[test="true"]{
color:green;}
QPushButton#button2[test="false"]{
color:red;}
2. Define attributes and implementation in the main class
#define POLISH(widget) (widget->style()->polish(widget))
class Widget : pulib QWidget
{
Q_OBJECT
Q_PROPERTY(bool test READ getTest WRITE setTest)
public:
Widget(QWidget *parent = nullptr) : QWidget(parent){
}
protected:
bool getTest(){
return test;}
void setTest(const bool &b){
test = b;}
protected slots:
// button1点击测试 - button2显示样式
void on_button1_clicked();
{
static bool b = false;
b = !b;
ui.button2->setProperty("test",b);
// 设置完属性想要button2更改样式就必须进行刷新
// 方案1:
ui.button2->style()->polish(ui.button2)
// 方案2 (注意传入为指针,该指针不能为空)
// POLISH(ui.button2);
}
private:
bool test = false;
}
Mention
It seems that you don't need to set Q_PROPERTY. Just set the attribute value for the corresponding control, wow.
reference
Can refer to Qt properties
attention
Search " Qt_io_ " or " Qt Developer Center " on WeChat public account to learn more about Qt and C++ development knowledge.
Author-jxd