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. [Solved] Confused about ownership of widgets in a layout
Forum Update on Monday, May 27th 2025

[Solved] Confused about ownership of widgets in a layout

Scheduled Pinned Locked Moved General and Desktop
qlayoutqlayoutitem
3 Posts 2 Posters 2.6k 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.
  • J Offline
    J Offline
    Jakob
    wrote on last edited by Jakob
    #1

    Today I got confused about who is supposed to own widgets that are managed by a layout.

    According to the Layout Documentation (http://doc.qt.io/qt-5/layout.html):

    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
    

    However, looking at the different examples of takeAt() that are available for instance here (http://doc.qt.io/qt-5/qlayout.html#takeAt) and there (http://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-example.html) and additionally the destructor of the FlowLayout class, the layout classes are responsible for deleting widgets, which seems to contradict exactly the note above.

    So, which one is correct? Is the note above a mistake?

    1 Reply Last reply
    0
    • O Offline
      O Offline
      onek24
      wrote on last edited by onek24
      #2

      @Jakob

      Afaik it deletes only a QLayoutItem. The Widgets created in the window.cpp of your example doesn't contain a parent. That means that it destroys the QLayoutItem, but the widgets are only getting destroyed on application exit. I might be wrong but that's what i see on first look.

      You can check it by keeping a reference to the widgets and destroy the FlowLayout by hand. If the widgets aren't destroyed, then your FlowLayout just destroys the QLayoutItem but keeps the widgets alive.

      J 1 Reply Last reply
      1
      • O onek24

        @Jakob

        Afaik it deletes only a QLayoutItem. The Widgets created in the window.cpp of your example doesn't contain a parent. That means that it destroys the QLayoutItem, but the widgets are only getting destroyed on application exit. I might be wrong but that's what i see on first look.

        You can check it by keeping a reference to the widgets and destroy the FlowLayout by hand. If the widgets aren't destroyed, then your FlowLayout just destroys the QLayoutItem but keeps the widgets alive.

        J Offline
        J Offline
        Jakob
        wrote on last edited by
        #3

        @onek24 You're absolutely right. I didn't yet grasp that the QLayoutItem in fact behaves as a kind of proxy for the actual widget (or child-layout). Thanks for the clarification.

        1 Reply Last reply
        0

        • Login

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