Python QPushButton setIcon: put icon on button


Question

I want to put an in ICON into a push button.. the code should work like that:

    self.printButton = QtGui.QPushButton(self.tab_name)
    self.printButton.setIcon(QtGui.QPixmap('printer.tif'))
    self.printButton.setGeometry(QtCore.QRect(1030, 500, 161, 61))

But instead, it gives the error message:

    TypeError: argument 1 of QAbstractButton.setIcon() has an invalid type

What is missing here?

All comments and suggestions are highly appreciated.

1
2
12/15/2009 5:46:08 AM

Accepted Answer

This is strange, I quickly tested the code on my C++ application and it seems to be working...

Maybe by using this you could correct your problem :

rMyIcon = QtGui.QPixmap("printer.tif");
self.printButton.setIcon(QtGui.QIcon(rMyIcon))

Hope this helps a bit...

9
12/15/2009 6:53:05 AM

Create a QIcon rather than a QPixmap for passing to setIcon(). Try changing the second line to

self.printButton.setIcon(QtGui.QIcon('printer.tif'))

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon