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.
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)