[PyQt] Addition of parent argument when subclassing with super.

Chris O'Halloran cmoman at gmail.com
Thu May 28 00:44:42 BST 2015


class SLD_sequence_scene(QtGui.QGraphicsScene):

    def __init__(self,x,y,w,h,*parent=None*):
        super(SLD_sequence_scene, self).__init__(x,y,w,h*,parent*) #  to
enable the program to exit gracefully

        print("the scene parent is {0}".format(self.parent()))


class SLD_label(QtGui.QLineEdit):
    def __init__(self):
        super(SLD_label, self).__init__()
        self.setGeometry(0,0,70,15)



class SLD_sequence_view(QtGui.QGraphicsView):

    def __init__(self, parent=None):
        super(SLD_sequence_view, self).__init__(parent)

        self.setGeometry(0,0,1000,1000)
        self.scene = SLD_sequence_scene(0,0,1000,900*,self*)
        self.setScene(self.scene)
        #self.d={}

Hello all,

I was trying to track down why my program wouldn't shut-down properly and I
traced it to requiring the above additions in* bold*.  Since I was hooking
into power system simulators and third-party graphic libraries it took a
wee while to trace :).

The fix above was a bit of guess (the bits in bold) since the PyQt
documentation often talks about *self* being owned by Qt instead of PyQt
but I'd seen it in other code so I tried it.  Now I'm more interested to
know why so I that I apply this technique in the proper manner.

For example

QGraphicsScene.__init__ (*self*, float *x*, float *y*, float *width*, float
*height*, QObject <http://pyqt.sourceforge.net/Docs/PyQt4/qobject.html>
*parent* = None)

***** The *parent* argument, if not None, causes *self* to be owned by Qt
instead of PyQt.<<<-----------****

Constructs a QGraphicsScene
<http://pyqt.sourceforge.net/Docs/PyQt4/qgraphicsscene.html> object, using
the rectangle specified by (*x*, *y*), and the given *width* and *height*
for its scene rectangle. The *parent* parameter is passed to QObject
<http://pyqt.sourceforge.net/Docs/PyQt4/qobject.html>'s constructor.

I'm sure the line "The *parent* argument, if not None, causes *self* to be
owned by Qt instead of PyQt." is probably true but it sure is confusing for
a beginner/intermediate programmer. My thinking goes "Is being owned by Qt
bad?  My code is using PyQt so I seem to be connecting to another sub
system.  If this is not bad, why do I need to know this?. In what context
would I need to be aware of this?"

Returning to my example above, *without* the addition in bold above the
line in my code that reads

print("the scene parent is {0}".format(self.parent())) -------------->
returns None

*With* the addition in bold above

print("the scene parent is {0}".format(self.parent())) ---------------->
returns the "scene parent is <sequence_diagrams.SLD_sequence_view object at
0x00000000051648B8>"

That all seems to make sense.  When the QGraphicsView is destroyed by the
QApplication being terminated, the QGraphicsScene is then garbage collected
because its parent has died.  Whereas without a parent, the program leave
the QGraphicsScene object alive and so the program won't terminate when
QApplication finishes.

Can anyone point me to some documentation that describes what is going on
with super and parent=None in the PyQt context, please?  I'd just like to
become a bit more comfortable with it.

Cheers all,

Chris O'Halloran
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150528/2597bd9e/attachment-0001.html>


More information about the PyQt mailing list