Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Does QLayoutItem delete its managed item in its destructor?
QtWS25 Last Chance

Does QLayoutItem delete its managed item in its destructor?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlayoutitem
4 Posts 2 Posters 841 Views
  • 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.
  • - Offline
    - Offline
    -._.-
    wrote on 1 Aug 2021, 14:48 last edited by
    #1

    I think the title states the question clearly.
    Background:
    Qt version 5.15
    The documentation of QLayoutItem doesn't say anything, but the documentation of QLayout::takeAt has an example code fragment that suggests that the managed item will not be destroyed.
    However checking my own program the debugger stack trace seems to indicate that it will be destroyed.

    I know that the answer is in the code, but sometimes Qt code can be very hard to read, so I'm hoping that someone knows the answer.

    P 1 Reply Last reply 1 Aug 2021, 15:18
    0
    • - -._.-
      1 Aug 2021, 14:48

      I think the title states the question clearly.
      Background:
      Qt version 5.15
      The documentation of QLayoutItem doesn't say anything, but the documentation of QLayout::takeAt has an example code fragment that suggests that the managed item will not be destroyed.
      However checking my own program the debugger stack trace seems to indicate that it will be destroyed.

      I know that the answer is in the code, but sometimes Qt code can be very hard to read, so I'm hoping that someone knows the answer.

      P Offline
      P Offline
      Pl45m4
      wrote on 1 Aug 2021, 15:18 last edited by Pl45m4 8 Jan 2021, 15:19
      #2

      QLayoutItem does not delete anything, same as all QLayouts.
      QLayoutItems are not even QObjects, so they cant have any QWidget child widgets, which they could delete in their d'tors.
      The parent widget, on which the layout is installed, can/will delete the child widgets.

      Tips for Using Layouts

      When you use a layout, you do not need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed.

      Note: Widgets in a layout are children of the widget on which the layout is installed, not of the layout itself. Widgets can only have other widgets as parent, not layouts.

      https://doc.qt.io/qt-5/layout.html#tips-for-using-layouts


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      - 1 Reply Last reply 2 Aug 2021, 21:55
      2
      • P Pl45m4
        1 Aug 2021, 15:18

        QLayoutItem does not delete anything, same as all QLayouts.
        QLayoutItems are not even QObjects, so they cant have any QWidget child widgets, which they could delete in their d'tors.
        The parent widget, on which the layout is installed, can/will delete the child widgets.

        Tips for Using Layouts

        When you use a layout, you do not need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed.

        Note: Widgets in a layout are children of the widget on which the layout is installed, not of the layout itself. Widgets can only have other widgets as parent, not layouts.

        https://doc.qt.io/qt-5/layout.html#tips-for-using-layouts

        - Offline
        - Offline
        -._.-
        wrote on 2 Aug 2021, 21:55 last edited by
        #3

        @Pl45m4 said

        QLayoutItem does not delete anything, same as all QLayouts.

        QLayoutItem is not a layout, rather, it is a layout item, so I don't understand your comment.
        On the contrary, layouts are also layout items (it is so in order to allow sublayouts).

        QLayoutItems are not even QObjects, so they cant have any QWidget child widgets, which they could delete in their d'tors.
        The parent widget, on which the layout is installed, can/will delete the child widgets.

        True, they are not QObjects, but they still have a destructor.
        And they still have a managed item (unless empty), which can be a widget, layout, or spacer, according to the documentation of QLayoutItem.

        Note: Widgets in a layout are children of the widget on which the layout is installed, not of the layout itself.

        This looks useful information.
        However, I never talked about widgets.
        In fact the layout item I was using was itself a layout, I didn't mention because I didn't think it would make any difference, now I understand I should have.
        If i understand that correctly, layouts delete their child layouts in their destructor.
        Is it done through the layout item destructor?

        P 1 Reply Last reply 3 Aug 2021, 09:06
        0
        • - -._.-
          2 Aug 2021, 21:55

          @Pl45m4 said

          QLayoutItem does not delete anything, same as all QLayouts.

          QLayoutItem is not a layout, rather, it is a layout item, so I don't understand your comment.
          On the contrary, layouts are also layout items (it is so in order to allow sublayouts).

          QLayoutItems are not even QObjects, so they cant have any QWidget child widgets, which they could delete in their d'tors.
          The parent widget, on which the layout is installed, can/will delete the child widgets.

          True, they are not QObjects, but they still have a destructor.
          And they still have a managed item (unless empty), which can be a widget, layout, or spacer, according to the documentation of QLayoutItem.

          Note: Widgets in a layout are children of the widget on which the layout is installed, not of the layout itself.

          This looks useful information.
          However, I never talked about widgets.
          In fact the layout item I was using was itself a layout, I didn't mention because I didn't think it would make any difference, now I understand I should have.
          If i understand that correctly, layouts delete their child layouts in their destructor.
          Is it done through the layout item destructor?

          P Offline
          P Offline
          Pl45m4
          wrote on 3 Aug 2021, 09:06 last edited by Pl45m4 8 Mar 2021, 09:07
          #4

          @_ said in Does QLayoutItem delete its managed item in its destructor?:

          @Pl45m4 said

          QLayoutItem does not delete anything, same as all QLayouts.

          QLayoutItem is not a layout, rather, it is a layout item, so I don't understand your comment.
          On the contrary, layouts are also layout items (it is so in order to allow sublayouts).

          It means what it says.

          @_ said in Does QLayoutItem delete its managed item in its destructor?:

          If i understand that correctly, layouts delete their child layouts in their destructor.
          Is it done through the layout item destructor?

          Check it yourself.

          QLayoutItem destructor does basically nothing, except destroying the QLayoutItem itself

          • https://code.woboq.org/qt5/qtbase/src/widgets/kernel/qlayoutitem.cpp.html#_ZN11QLayoutItemD1Ev

          whereas, if you delete a QLayout, it will delete all of its child-layouts, but not their widgets

          • https://code.woboq.org/qt5/qtbase/src/widgets/kernel/qlayout.cpp.html#_ZN7QLayoutD1Ev

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          2

          2/4

          1 Aug 2021, 15:18

          • Login

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