Hi guys,

I've a problem calling a 'parent' PyQt4 method from inside an object that inerits it.

Here is the code:

import sys
from PyQt4 import QtCore, QtGui

class ItiaQPushButton(QtGui.QPushButton):
	def __init__(self, txt):
		super(ItiaQPushButton, self).__init__("Hello")
		print super(ItiaQPushButton, self).__doc__
		#QtGui.QPushButton.setText("BO")
	def setText2(self, txt):
		self.setText(txt)
		#super(ItiaQPushButton, self).setText("PROVA")

class Window(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        flowLayout = QtGui.QHBoxLayout()
        b = ItiaQPushButton("Test")
        b.setText2(self.tr("qwe"))
        flowLayout.addWidget(b)        
        self.setLayout(flowLayout)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    mainWin = Window()
    mainWin.show()
    sys.exit(app.exec_())

This works but if I try to uncomment the command 'super(ItiaQPushButton, self).setText("PROVA")' it doesn't work anymore. I would like to change the name 'setText2' into 'setText' but without calling setText method through 'super' command I can't call the inherited 'setText' method.

I hope someone can help me,
Thank you

Matteo

I forgot:

super, as shown by the code, works for __init__ and __doc__, but not for other inherited methods.

Thank you!!

It doesn't work if I rename he method 'setText2' into 'setText' since it would try to call itself recursively, without accessing the super's one.

Yeeees,

it works, but I really am not sure why.

Thank you!

To know why, you could try print(QtGui.QPushButton.setText) or print(type(QtGui.QPushButton.setText)) . However, I don't like super . It's not a very useful feature.

This question has already been answered. Start a new discussion instead.