0

I am setting image (25x25 original size) on a QPushButton like this:icon =

QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("pause.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton = QtGui.QPushButton()
        self.pushButton.setIcon(icon)

also tried this:

self.pushButton.setIcon(QtGui.QIcon("pause.png"))

also "/pause.png" and nothing works. Im sure ive tried several more things i saw online and in all it compiles but there is no image on the button. Tried setting imagesize like this:

self.pushButton.setIconSize(QSize(24, 24))

and there is no change. Using pycharm + qt5.7

user5804127
  • 49
  • 11
  • If you are using images for icons I would strongly recommend using Qt's [resource collection files](http://doc.qt.io/qt-5/resources.html#resource-collection-files-op-op-qrc). This would not only make accessing the files easier but make your application more portable. – rbaleksandar Aug 29 '16 at 17:45

1 Answers1

0

You're assuming the current working directory of the process by providing a relative path to the image. As you've noticed, your assumption is wrong. Use an absolute path referenced to e.g. the application's location:

// Python
os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) 
  + QDir.separator() + "pause.png"`
// C++
QCoreApplication.applicationDirPath() + QDir.separator() + "pause.png"`
Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313