A problem when setting QIcon and stylesheet of QPushButton
-
Hi,
I got a problem when I tried to set 3 different looks for 3states of QPushButton : normal, pressed, disabled.I Only have 2 pictures for Normal & Pressed state. And I want the look of the disabled state to be like the Qt self does to a disabled QIcon, that is to grey the button. I've tried a lot of methods, they didn't work. Anybody knows how to do this stuff? Thanks.
-
Here is an example that I've got from designer.
It creates a new QPushButton and sets different colours for active, inactive and disabled state.
@
pushButton = new QPushButton(widgetPlayback);
pushButton->setObjectName(QStringLiteral("pushButton"));
pushButton->setGeometry(QRect(110, 210, 80, 23));
QPalette palette;
QBrush brush(QColor(54, 239, 156, 255));
brush.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Active, QPalette::Button, brush);
QBrush brush1(QColor(199, 239, 111, 255));
brush1.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Inactive, QPalette::Button, brush1);
QBrush brush2(QColor(190, 187, 184, 255));
brush2.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Disabled, QPalette::Button, brush2);
pushButton->setPalette(palette);
@ -
And "here":http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-a-qpushbutton-using-the-box-model
Is a well explained stylesheet example -
Thanks for your reply.
Yes, I know this could work for colors in stylesheet.
But I want use pictures instead of colors.
As I said, I only have two pngs , "A" for state Normal, and "B" for sate Pressed. I know that the Qt can grey a button icon (a png file set for this icon) when a button is disabled. So I want the disabled state for the button has icon of a greyed "A".
Any solution?
Thanks.
[quote author="andreyc" date="1395417004"]Here is an example that I've got from designer.
It creates a new QPushButton and sets different colours for active, inactive and disabled state.
@
pushButton = new QPushButton(widgetPlayback);
pushButton->setObjectName(QStringLiteral("pushButton"));
pushButton->setGeometry(QRect(110, 210, 80, 23));
QPalette palette;
QBrush brush(QColor(54, 239, 156, 255));
brush.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Active, QPalette::Button, brush);
QBrush brush1(QColor(199, 239, 111, 255));
brush1.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Inactive, QPalette::Button, brush1);
QBrush brush2(QColor(190, 187, 184, 255));
brush2.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Disabled, QPalette::Button, brush2);
pushButton->setPalette(palette);
@
[/quote] -
in the link i gave you: scroll down to the end and you will see this topic : QPushButton and images.
Border-image is the key to your solution.
You can use a border-image for the normal state and another one for the pressed state