views:

334

answers:

3

Hello Everybody,

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.

+3  A: 

Hi,

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...

Andy M
Both " and ' are valid in Python.
Georg
The implicit conversion of `QPixmap` to `QIcon` seems not to work. I've just tried this with PyQt 4.6, but it didn't work either.
Georg
It's strange yes... I never tried this, but, do you think it would be possible to contact directly Nokia about it ? Even if you (maybe) don't have a commercial licence...The following answer of baysmith should work as well !
Andy M
+3  A: 

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

self.printButton.setIcon(QtGui.QIcon('printer.tif'))
baysmith
A: 

Hi Baysmith and Andy... thanks for the input. I tested your suggestions, it worked. I also have to add setIconSize, otherwise the icon is displayed very small. Here is code:

def printerButton(self,tab_name):
    self.printButton = QtGui.QPushButton(tab_name)
    self.printButton.setIcon(QtGui.QIcon('icons/printer.tif'))
    self.printButton.setIconSize(QtCore.QSize(130,130))
    self.printButton.setGeometry(QtCore.QRect(1030, 500, 161, 61))

Hope this help others too....|:0),

ThreaderSlash
Cool... nice job, thanks for the feedback !
Andy M