Qt stylesheet selector using enumed properties

Viewed 710

I'm trying to set different visual styles for a pressed QToolButton depending on whether it displays a menu or not.

In my code, tool buttons having menu set their popupMode property to QToolButton::InstantPopup (value 2), while buttons without an associated menu keep the default value (QToolButton::DelayedPopup, value 0).

I tried to use such property in different ways as selector, but only the last one (QToolButton[popupMode="2"]) worked:

/* Not working */
QToolButton[popupMode=InstantPopup]:pressed,
QToolButton[popupMode="InstantPopup"]:pressed,
QToolButton[popupMode="QToolButton::InstantPopup"]:pressed,
QToolButton[popupMode="QToolButton--InstantPopup"]:pressed,
QToolButton[qproperty-popupMode=InstantPopup]:pressed,
QToolButton[qproperty-popupMode="InstantPopup"]:pressed,
QToolButton[qproperty-popupMode="QToolButton::InstantPopup"]:pressed,
QToolButton[qproperty-popupMode="QToolButton--InstantPopup"]:pressed,
QToolButton[qproperty-popupMode="2"]:pressed
{
    background-color: blue;
}
/* Working */
QToolButton[popupMode="2"]:pressed,
{
    background-color: red;
}

(This is a compilation of the options, I've tested them separately).

Documentation mentions that if the enum is declared using Q_ENUM (as ToolButtonPopupMode does), then it should be referenced by name, not by value, but, as it can be seen above, it seems it is not the case for selectors.


Question: Would it be possible to use such enum's name as selector in the stylesheet instead of the enum's value?


Note: I understand that other options such as custom properties with a more expressive, Qt-independant value can make the work too. I'm curious about the possibility of using the enum in the described way.

1 Answers

QToolButton[popupMode=InstantPopup]:pressed is the correct one.

setProperty(<property_name>, QVariant::fromValue(<enum_value>)) for enum class.

setProperty(<property_name>, <enum_value>) for enum.

But you need to reload the style if you want it to change dynamically. Read about QStyle::unpolish and QStyle::polish.

Related