Question

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.

Was it helpful?

Solution

button->setIcon(QIcon("/example/pics/example.png"));

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top