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

Recommended Answers

All 6 Replies

I forgot:

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

Thank you!!

Did you try

ItiaQPushButton.setText(self, "PROVA")

?

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.

Sorry, I meant that you should try

QtGui.QPushButton.setText(self, "PROVA")

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.