954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem calling 'super' inheriting PyQt4 objects

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

matMalo
Newbie Poster
5 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

I forgot:

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

Thank you!!

matMalo
Newbie Poster
5 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Did you try

ItiaQPushButton.setText(self, "PROVA")

?

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

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.

matMalo
Newbie Poster
5 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Sorry, I meant that you should try

QtGui.QPushButton.setText(self, "PROVA")
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

Yeeees,

it works, but I really am not sure why.

Thank you!

matMalo
Newbie Poster
5 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You