Today I had similar problem, maybe someone is still looking for solution. 
That's what I've made:
MyPushButton.h:
{
Q_OBJECT
Q_PROPERTY(QString style MEMBER _styleStr
)
public:
enum Style
{
StyleNormal = 1,
StyleOk,
StyleWarning,
StyleRed,
};
Q_ENUM(Style)
explicit MyPushButton
(QWidget *parent
= nullptr
);
Style style() const;
void setStyle(const Style &style);
private:
Style _style;
};
class MyPushButton : public QPushButton
{
Q_OBJECT
Q_PROPERTY(QString style MEMBER _styleStr)
public:
enum Style
{
StyleNormal = 1,
StyleOk,
StyleWarning,
StyleRed,
};
Q_ENUM(Style)
explicit MyPushButton(QWidget *parent = nullptr);
Style style() const;
void setStyle(const Style &style);
private:
Style _style;
QString _styleStr;
};
To copy to clipboard, switch view to plain text mode
MyPushButton.cpp:
...
MyPushButton::Style MyPushButton::style() const
{
return _style;
}
void MyPushButton::setStyle(const Style &style)
{
switch (style)
{
case StyleNormal: _styleStr = "StyleNormal"; break;
case StyleOk: _styleStr = "StyleOk"; break;
case StyleWarning: _styleStr = "StyleWarning"; break;
case StyleRed: _styleStr = "StyleRed"; break;
}
_style = style;
}
...
...
MyPushButton::Style MyPushButton::style() const
{
return _style;
}
void MyPushButton::setStyle(const Style &style)
{
switch (style)
{
case StyleNormal: _styleStr = "StyleNormal"; break;
case StyleOk: _styleStr = "StyleOk"; break;
case StyleWarning: _styleStr = "StyleWarning"; break;
case StyleRed: _styleStr = "StyleRed"; break;
}
_style = style;
}
...
To copy to clipboard, switch view to plain text mode
styles.css:
...
MyPushButton[style="StyleRed"]
{
background-color: #ff0000;
...
}
...
...
MyPushButton[style="StyleRed"]
{
background-color: #ff0000;
...
}
...
To copy to clipboard, switch view to plain text mode
It works very well.
Bookmarks