Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Python/PyQt freeing of objects
Forum Updated to NodeBB v4.3 + New Features

Python/PyQt freeing of objects

Scheduled Pinned Locked Moved Solved Language Bindings
5 Posts 2 Posters 2.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JonB
    wrote on 8 Jan 2018, 10:01 last edited by
    #1

    I have inherited a large body of Python/PyQt code. I note the following:

    • Some QDialogs have QtCore.Qt.WA_DeleteOnClose set, while some don't.
    • Some QWidget/QBoxLayouts are created via QWidget(self) (where self is the QDialog), while some (most) are created via plain QWidget().

    Bear in mind that all Python/PyQt objects are created on the heap not the stack (i.e. C++ QWidget* w = new QWidget() not QWidget w). I am aware that in ("unmanaged") C++ it is the caller's responsibility to delete such newed objects, else they leak. In Python, however, I understand it to be "managed" (i.e. reference counting), so it will delete referenced objects once they go out of scope (right?).

    So, my question is: is it OK to let Python manage all this and not have to worry about closing or passing a parent, without any leaking? I am not asking about "best practice" here, I need a definitive understanding of current behaviour as there are hundreds of such calls with no seeming consistency.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 8 Jan 2018, 21:42 last edited by
      #2

      Hi,

      Widgets put in a layout get reparented to the widget where the layout is set so they don't need the self parameter when created.

      Layouts which get a parent on construction are set on that widget. It saves a call to setLayout(whateverLayout).

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply 9 Jan 2018, 08:07
      0
      • S SGaist
        8 Jan 2018, 21:42

        Hi,

        Widgets put in a layout get reparented to the widget where the layout is set so they don't need the self parameter when created.

        Layouts which get a parent on construction are set on that widget. It saves a call to setLayout(whateverLayout).

        J Offline
        J Offline
        JonB
        wrote on 9 Jan 2018, 08:07 last edited by JonB 1 Sept 2018, 08:09
        #3

        @SGaist
        Thank you. With all due respect (and you deserve a lot of that!), that was not the question. The question is: when using Python/PyQt, do I ever have to care whether widgets/layouts do or do not get reparented, or can I rely on Python's reference counting & disposal system to do all necessary clean up for me (in a way that does not apply to [unmanaged] C++) ? That's why this question is in the Language Bindings section of the forum.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 9 Jan 2018, 23:00 last edited by
          #4

          This stackoverflow answer contains a pretty nice explanation.

          Short version: do care about that properly. There are two counting involved: the one from python and the one from Qt (parent/child relationship).

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply 9 Jan 2018, 23:02
          2
          • S SGaist
            9 Jan 2018, 23:00

            This stackoverflow answer contains a pretty nice explanation.

            Short version: do care about that properly. There are two counting involved: the one from python and the one from Qt (parent/child relationship).

            J Offline
            J Offline
            JonB
            wrote on 9 Jan 2018, 23:02 last edited by
            #5

            @SGaist
            Great stuff --- I'll study that link.

            1 Reply Last reply
            0

            1/5

            8 Jan 2018, 10:01

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved