Search code examples
c++qtqtstylesheets

Properties combination in Qt stylesheet


I wrote Qt4 (or Qt5) class MyButton and defined two boolean properties, like this:

#include <QPushButton>

class MyButton : QPushButton
{
  Q_OBJECT
  Q_PROPERTY(bool property_1 READ property_1)
  Q_PROPERTY(bool property_2 READ property_2)
public:
  explicit MyButton(QWidget *parent = 0);
  ...
}

Now I want to customize application stylesheet in external file so that in different combinations of this properties MyButton has different background color. Separately this works well:

MyButton[property_1="true"] { background-color: black }
MyButton[property_2="true"] { background-color: white }

So the question is: how to combine few properties in the same condition with "and", "or" and "not" operations?


Solution

  • The idea is the same as CSS attribute selection.

    Thus property_1="true" AND property_2="true" condition is:

    MyButton[property_1="true"][property_2="true"] { background-color: green; }