How to set image on QPushButton?

Qt

Qt Problem Overview


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.

Qt Solutions


Solution 1 - Qt

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());

Solution 2 - Qt

QPushButton *button = new QPushButton;
button->setIcon(QIcon(":/icons/..."));
button->setIconSize(QSize(65, 65));

Solution 3 - Qt

You can also use:

button.setStyleSheet("qproperty-icon: url(:/path/to/images.png);");

Note: This is a little hacky. You should use this only as last resort. Icons should be set from C++ code or Qt Designer.

Solution 4 - Qt

You may also want to set the button size.

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

Solution 5 - Qt

I don't think you can set arbitrarily sized images on any of the existing button classes. If you want a simple image behaving like a button, you can write your own QAbstractButton-subclass, something like:

class ImageButton : public QAbstractButton {
Q_OBJECT
public:
...
    void setPixmap( const QPixmap& pm ) { m_pixmap = pm; update(); }
    QSize sizeHint() const { return m_pixmap.size(); }
protected:
    void paintEvent( QPaintEvent* e ) {
        QPainter p( this );
        p.drawPixmap( 0, 0, m_pixmap );
    }
};

Solution 6 - Qt

This is old but it is still useful, Fully tested with QT5.3.

Be carreful, example concerning the ressources path :

In my case I created a ressources directory named "Ressources" in the source directory project.

The folder "ressources" contain pictures and icons.Then I added a prefix "Images" in Qt So the pixmap path become:

QPixmap pixmap(":/images/Ressources/icone_pdf.png");

JF

Solution 7 - Qt

You can do this in QtDesigner. Just click on your button then go to icon property and then choose your image file.

Solution 8 - Qt

Just use this code

QPixmap pixmap("path_to_icon");
QIcon iconBack(pixmap);

Note that:"path_to_icon" is the path of image icon in file .qrc of your project You can find how to add .qrc file https://stackoverflow.com/questions/17806552/including-images-and-a-file-to-a-project">here</a>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiongreshi GuptaView Question on Stackoverflow
Solution 1 - QtJérômeView Answer on Stackoverflow
Solution 2 - QtSharanabasu AngadiView Answer on Stackoverflow
Solution 3 - QtIuliuView Answer on Stackoverflow
Solution 4 - QtJonathanView Answer on Stackoverflow
Solution 5 - QtFrank OsterfeldView Answer on Stackoverflow
Solution 6 - QtJFP74View Answer on Stackoverflow
Solution 7 - QtNemeth AttilaView Answer on Stackoverflow
Solution 8 - QtdqtheView Answer on Stackoverflow