Python QPushButton setIcon: put icon on button
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 in开发者_JAVA技巧valid type
What is missing here?
All comments and suggestions are highly appreciated.
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...
Create a QIcon rather than a QPixmap for passing to setIcon(). Try changing the second line to
self.printButton.setIcon(QtGui.QIcon('printer.tif'))
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),
精彩评论