Solved QPushButton: changing icon via setStyleSheet
-
Hi!
How to change QPushButton icon in runtime?
I try do something like this:b4 = new QPushButton; b4->setObjectName("bEthOn_"+QString::number(parent_layout->count())); b4->setIconSize(QSize(16, 16)); b4->setCheckable(true); b4->setStyleSheet("QPushButton:checked { qproperty-icon: url(:/icons/icons/eth_play.png); } QPushButton:unchecked { qproperty-icon: url(:/icons/icons/eth_pause.png); }");
But it doesn't working.
-
@sitesv
Hi
Well its due to the use of qproperty-icon
as background-image, border-image, and image works.
Just not the icon :)Its a limitation/optimaization.
-
are your icon files set in qrc file?
-
Yes. Icons in qrc
-
@sitesv
there is no conversion for QIcon type via stylesheets properties.
Yo ucan create a custom QString-typed QProperty and "forward" it to the QPushButton's icon property. -
@raven-worx
Not really understand your solution.
I can achieve the icon to be displayed by stylesheet method, but it doesn’t change by clicking -
Hi
That stylesheet styles the Checked state and not pressed
so for it to work - the button must have Checkable set to true.
(its a property)So i think you might want the pressed state instead
QPushButton { background-color: rgb(51, 153, 204); } QPushButton:pressed { background-color: rgb(224, 0, 0); }
But it wont make you happy as docs says
"Note: Use the qproperty syntax with care, as it modifies the widget that is being painted. Also, the qproperty syntax is evaluated only once,"so it wont work to change icon when clicked
-
@mrjj
Hi!
I'm already set Checkable property and want to use checked/unchecked property.
And I don't understand why changing background color via stylesheet works, but changing icon - doesn't. -
@sitesv
Hi
Well its due to the use of qproperty-icon
as background-image, border-image, and image works.
Just not the icon :)Its a limitation/optimaization.
-
@sitesv there is no
unchecked
property (afaik)use
QPushButton { ... }
andQPushButton:checked { ... }
as identifiers -
b4->setStyleSheet("QPushButton { qproperty-icon: url(:/icons/icons/eth_play.png); } QPushButton:checked { qproperty-icon: url(:/icons/icons/eth_pause.png); }");
doesn't work. No any icon on pushbutton shows...
b4->setStyleSheet("QPushButton { qproperty-icon: url(:/icons/icons/eth_play.png); }");
This icon is displayed fine