PDA

View Full Version : Q_ENUMS and Qt Style Sheet



Funtast
6th September 2012, 20:17
Well, strange things in QT happens when I try to load stylesheet, located in separate file

I have a class looks like following:

AClass.h:


class AClass : public QFrame
{
Q_OBJECT
Q_ENUMS(State)
Q_PROPERTY(State status READ getStatus WRITE setStatus)
Q_PROPERTY(int status_int READ getStatus_int WRITE setStatus_int)
public:
enum State {Ok,Wrong,Unknown};
State getStatus() const {return m_status;}
void setStatus(State status) {m_status=status;}
int getStatus_int() const {return m_status_int;}
void setStatus_int(int status) {m_status_int=status;}
...
private:
State m_status;
int m_status_int;
...
};

AClass.cpp:


AClass::AClass(QWidget *parent) :
QFrame(parent),
m_status(Ok),
m_status_int(0)
{
}
...


Lines in .css file:

#AClass[status="Ok"] {background-color: lime;} //////////// DO NOT WORK
#AClass[status_int="0"] {{background-color: lime;} /////////// EVERYTHING OK!

Well, in Qt documentation written:
"If the property references an enum declared with Q_ENUMS,
you should reference its constants by name, i.e., not their numeric value."
It seems to me it does not work at all...:confused:
Possibly I'm too stupid to understand Q_ENUMS macros... :)
Please! Help me anybody)

pgalyen
8th October 2015, 20:10
Hey I know I am about 3 years to late but did you ever figure this one out? I am currently having the same problem.

jasio
17th March 2019, 21:56
Today I had similar problem, maybe someone is still looking for solution. ;)
That's what I've made:

MyPushButton.h:


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;
};

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;
}
...

styles.css:

...
MyPushButton[style="StyleRed"]
{
background-color: #ff0000;
...
}
...

It works very well.