0

I'm fairly new to C++ in general, so I need a little help with Qt. I'm trying to add an image to a PushButton, and I keep having problems with it. Here is an example of what I have:

#include <QtWidgets/QPushButton>
QPushButton *button;
button = new QPushButton(Example);
button->setObjectName(QStringLiteral("button"));
button->setGeometry(0,0,128,56);

So I have a picture saved in /example/pics/example.png (example being the project name), and I would like to use it on the PushButton. I've been messing around with it for a while, and can't find a solution, so any help is appreciated.

Connor M
  • 331
  • 3
  • 5
  • 18
  • Whether you want to load image as part of resource file( exe/dll) or directly load from local disk while loading the image ? – Ashif Aug 20 '13 at 06:44

2 Answers2

4
button->setIcon(QIcon("/example/pics/example.png"));
Davy Jones
  • 621
  • 6
  • 4
  • I tried adding this to my code, but it didn't seem to do anything. The PushButton is still blank. – Connor M Aug 20 '13 at 06:41
  • Oh, never mind, just figured out the problem, I was screwing up the path to the .png file! Thanks for the help! – Connor M Aug 20 '13 at 06:46
0

In pyqt5/pyside2, this is what I used:

icon = QIcon()
pixmap = QPixmap(r'C:\Users\git\Desktop\test.png').scaled(QSize(160, 90))
icon.addPixmap(pixmap, QtGui.QIcon.Normal, QtGui.QIcon.Off)
pushButton.setIcon(icon)
pushButton.setIconSize(QSize(160, 90))
pushButton.setStyleSheet("QPushButton{border-radius:5px;border: 1px solid #345781;}") 

Screenshot of the result

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
JTL
  • 41
  • 3