105

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.

Smi
  • 13,850
  • 9
  • 56
  • 64
greshi Gupta
  • 1,191
  • 3
  • 8
  • 6
  • 3
    there are two ways using which you can set the image on a button in Qt, Programmatic way of setting image, http://qt-articles.blogspot.com/2010/06/how-to-customize-button-in-qt.html from style sheet how the image is being set, http://qt-articles.blogspot.com/2010/06/how-to-add-stylesheet-for-button-in-qt.html – Naruto Jun 30 '10 at 05:18
  • You are most welcome :) if you feel the answer is correct, please mark it as right so it would helpful for other who gets the similar problem. – Naruto Jul 01 '10 at 12:19

9 Answers9

77

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());
Jérôme
  • 26,567
  • 29
  • 98
  • 120
59
QPushButton *button = new QPushButton;
button->setIcon(QIcon(":/icons/..."));
button->setIconSize(QSize(65, 65));
user1119279
  • 331
  • 3
  • 9
Sharanabasu Angadi
  • 4,304
  • 8
  • 43
  • 67
11

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.

Iuliu
  • 4,001
  • 19
  • 31
10

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());
Jonathan
  • 6,741
  • 7
  • 52
  • 69
6

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 );
    }
};
Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
5

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

Nemeth Attila
  • 133
  • 2
  • 13
  • Do this with the base 'icon' setting, at least to see it working with your image, before trying to set the icon per state. Set the size of the icon to be that of your image, or at least large enough for your image, or it will not display. – ClearCrescendo Aug 15 '20 at 18:18
  • This doesn't work as the "icon" part asks you to put on some themes. QT6.1. I thought it would open a dialog and I can choose an image file but not really. – Nicholas Humphrey Jul 01 '21 at 03:57
3

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

JFP74
  • 39
  • 1
1

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 here

AAEM
  • 1,837
  • 2
  • 18
  • 26
dqthe
  • 683
  • 5
  • 8
1

In case anybody needs a PyQt version of the first answer:

class PictureButton(QAbstractButton):

  def __init__(self, picture, parent):
    super().__init__(parent)
    self.setPicture(QPixmap(picture))

  def setPicture(self, picture):
    self.picture = picture
    self.update()

  def sizeHint(self):
    return self.picture.size()

  def paintEvent(self, e):
    painter = QPainter(self)
    painter.drawPixmap(0, 0, self.picture)
Tom
  • 834
  • 1
  • 14
  • 24