4

I have two classes :

The first one (the parent class) instantiates the child class in a method. I am trying to modify parent objects properties inside the child class. (These objects are PyQT QWidget s).

Here is the begining of my Parent and Child classes :

Parent :

class ParentClass(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(ParentClass, self).__init__()

        somevar=1200

        self.ChildItem=ChildClass()
        self.ChildItem.Start(somevar)

Child :

class ChildClass(QtGui.QWidget):
    def Start(self, var):
        super(ChildClass, self).__init__(self)

        self.parent().resize(var,var)

However, even if I have no errors, the last line produces nothing.

I've seen on several examples that the super() could be used to call parent methods, so I assume that it would be a solution for my case. However, I was unable to make it work either.

I have a lot of troubles understanding the super(), it always get into complicated concepts such as multiple inheritance when I just want to do a simple thing.

Manjunath N
  • 1,365
  • 11
  • 22
Olivier Giniaux
  • 870
  • 2
  • 8
  • 22

2 Answers2

2

Parent in Qt terminilogy is not parent in python terminology, from docs:

Returns a pointer to the parent object.

QObjects organize themselves in object trees. When you create a QObject with another object as parent, the object will automatically add itself to the parent's children() list.

But parent here is in Qt terminology, an object to which this object belongs, this has nothing to do with python inheritance.

For python inheritance you need super:

super(ParentClass, self).method()

Which is trivial in case of single inheritance.

Community
  • 1
  • 1
Andrey
  • 59,039
  • 12
  • 119
  • 163
  • Uh ok I wasn't aware that parent() was a Qt thing. It explains a lot. I've tried to do some research about this but searching "parent()" on google is like searching for "parent" which makes things harder. Thank you for your help. – Olivier Giniaux Jul 06 '15 at 09:34
  • 1
    @OdgyGsf yeah looking for such general tems on google may not yield result. If you sure that this method exists you better search your library documentation first before google. – Andrey Jul 06 '15 at 09:38
  • In either way, it is not working for me. super(ParentClass, self).method() returns an error (ParentClass is not defined). I am completly off the track with the super() thing – Olivier Giniaux Jul 06 '15 at 09:54
  • 1
    @OdgyGsf where are you calling it? First parameter in super is the current class you calling it from, I just gave example. Also you need to call via super to avoid calling overloaded version, are you sure that it is what you need? – Andrey Jul 06 '15 at 10:07
  • I am trying to call a parent's method from this child class (or 'subclass'). ParentClass contains a `updateStatus(progress)` method, And I try to access it with `super(ChildClass, self).updateStatus(x)` (but it now says that super has no attribute 'updateStatus'). Concretely, this child class is a window that takes some time to load. I try to show the progress of the loading in the parent window. – Olivier Giniaux Jul 06 '15 at 10:13
  • 1
    @OdgyGsf are you sure that your classes are actually parent and child in terms of python? – Andrey Jul 06 '15 at 10:52
  • Indeed, it looks like my child class is simply a subclass and not really a child class. There is no inheritance. – Olivier Giniaux Jul 06 '15 at 11:08
  • 1
    Subclass is a child class. – Andrey Jul 06 '15 at 11:19
  • OK. I've been reading a lot of tutorials and they all explains inheritance. But there is no inheritance in what I am doing. This child class is not derived from it's parent and has nothing in common with it. So for me doing `class ChildClass(ParentClass):` wouldn't makes sense. It is just possible to still access my updateStatus in my case ? – Olivier Giniaux Jul 06 '15 at 12:00
  • I sticked with the parent(). Lol. – Olivier Giniaux Jul 06 '15 at 12:28
  • I have hard time following your though. – Andrey Jul 06 '15 at 12:41
2

The problem here has got nothing to do with super, or inheritance. With inheritance, the parent/child relationship is between classes. But in your example, ChildClass doesn't inherit ParentClass, so that is not relevant.

There is another kind of parent/child relationship in Qt, though, and that is between instances. The constructors for widgets have an argument that allows you to pass in a reference to a parent object (which is usually another widget instance). However, the argument is optional, so if you don't explicitly set it, calling parent() afterwards would just return None.

The specific error in your example code, is that the child is never given a parent.

Here is what your example should look like:

class ParentClass(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(ParentClass, self).__init__()    
        # pass in "self" as the parent
        self.ChildItem = ChildClass(self)
        self.ChildItem.Start(1200)

class ChildClass(QtGui.QWidget):
    def Start(self, var):
        self.parent().resize(var, var)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336