Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set image on QPushButton?

Tags:

qt

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.

like image 495
greshi Gupta Avatar asked Jun 29 '10 04:06

greshi Gupta


People also ask

How to add image to QPushButton?

Just click on your button then go to icon property and then choose your image file.

How do I use Qt icons?

To use these icons within Qt Designer you would select the drop-down and choose "Set Icon From Theme..." You then enter the name of the icon you want to use, e.g. document-new (the full list of valid names). If you're not using designer, you can set icons from a theme in your code using the following.

What is QToolButton?

The QToolButton class provides a quick-access button to commands or options, usually used inside a QToolBar.


2 Answers

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()); 
like image 165
Jérôme Avatar answered Sep 22 '22 19:09

Jérôme


QPushButton *button = new QPushButton; button->setIcon(QIcon(":/icons/...")); button->setIconSize(QSize(65, 65)); 
like image 26
Sharanabasu Angadi Avatar answered Sep 22 '22 19:09

Sharanabasu Angadi