How to set image on QPushButton?

qt
greshi Gupta picture greshi Gupta · Jun 29, 2010 · Viewed 143.6k times · Source

I want to set an image on QPushButton, and the size of QPushButton should depend on the size of the image. I am able to do this when using QLabel, but not with QPushButton.

So, if anyone has a solution, then please help me out.

Answer

Jérôme picture Jérôme · Jun 30, 2010

What you can do is use a pixmap as an icon and then put this icon onto the button.

To make sure the size of the button will be correct, you have to reisze the icon according to the pixmap size.

Something like this should work :

QPixmap pixmap("image_path");
QIcon ButtonIcon(pixmap);
button->setIcon(ButtonIcon);
button->setIconSize(pixmap.rect().size());