python - From where comes the parent argument (PySide)? -
in example, comes parent
argument, provides it?
class mainwindow(qtgui.qmainwindow): def __init__(self): super(mainwindow, self).__init__() self.do_something() #sanity check self.cw = childwidget(self) self.setcentralwidget(self.cw) self.show() def do_something(self): print 'doing something!' class childwidget(qtgui.qwidget): def __init__(self, parent): super(childwidget, self).__init__(parent) self.button1 = qtgui.qpushbutton() self.button1.clicked.connect(self.do_something_else) self.button2 = qtgui.qpushbutton() self.button2.clicked.connect(self.parent().do_something) self.layout = qtgui.qvboxlayout() self.layout.addwidget(self.button1) self.layout.addwidget(self.button2) self.setlayout(self.layout) self.show() def do_something_else(self): print 'doing else!'
parent , children specific qt (c++). doc of qobject:
qobjects organize in object trees. when create qobject object parent, object automatically add parent's children() list. parent takes ownership of object; i.e., automatically delete children in destructor.
qwidget
, lot of other class inherits fromqobject
, apply them too. every children, parent()
method returns pointer parent object.
basically, creates widget parents can deleted properly. common case, main window parent - or grandparent - of widgets: when close window, deleted in right order.
from comment, think confused use ofsuper()
. not call parent of widget.
another way write this:
class childwidget(qtgui.qwidget): def __init__(self, parent): super(childwidget, self).__init__(parent)
is call init method of qwidget
directly:
class childwidget(qtgui.qwidget): def __init__(self, parent): qtgui.qwidget.__init__(parent)
childwidget
inherits qwidget
(that's how defined class). in init method, need call default constructor of qwidget
. otherwise won't able use default methods , attributes of qwidget
(try , see...).
if given parent, organize accordingly. add parent children
method, , keep reference parent.
Comments
Post a Comment