I'm trying to use a dynamic property to change a style for a QPushButton.
I have something like the following:
C++
// Create button with dynamic property
button
->setObjectName
(QString::fromUtf8("MyButton"));
button->setProperty("myProperty", "rank1");
// Button background should be red now
...
// Change the property
button->setProperty("myProperty", "rank2");
// The button's background colour should be blue now
// Create button with dynamic property
button = new QPushButton(this);
button->setObjectName(QString::fromUtf8("MyButton"));
button->setProperty("myProperty", "rank1");
// Button background should be red now
...
// Change the property
button->setProperty("myProperty", "rank2");
// The button's background colour should be blue now
To copy to clipboard, switch view to plain text mode
QSS
QPushButton#MyButton[myProperty="rank1"] { background: red; } QPushButton#MyButton[myProperty="rank2"] { background: blue; }
QPushButton#MyButton[myProperty="rank1"] { background: red; }
QPushButton#MyButton[myProperty="rank2"] { background: blue; }
To copy to clipboard, switch view to plain text mode
This does work to a certain degree. If I call QApplication::setStyleSheet() after updating myProperty, the style does update correctly. But the style is not updating correctly when I change the property. Is it incorrect to expect the button's background colour to change dynamically when the "myProperty" dynamic property is changed?
Bookmarks