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. QLayout retroactive parenting?
Qt 6.11 is out! See what's new in the release blog

QLayout retroactive parenting?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 96 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.
  • E Offline
    E Offline
    Estelyen
    wrote last edited by
    #1

    I know that adding a Widget (let's say a QPushButton) to a QLayout will automatically set its parent to the Widget the layout is installed in (let's say a QDialog):

    QDialog* dlg { new QDialog() };
    QVBoxLayout* layout { new QVBoxLayout(dlg) };
    QPushButton* btn { new QPushButton("Click me!") };
    layout->addWidget(btn); // dlg is set as parent of btn here
    

    But if the layout is created parentless, will it retroactively assign parentage once I add it to the Dialog?

    QVBoxLayout* layout { new QVBoxLayout() }; // note: Not assigned to any widget yet
    QPushButton* btn { new QPushButton("Click me!") };
    layout->addWidget(btn); // btn cannot be assigned a parent
    ...
    QDialog* dlg { new QDialog() };
    dlg->setLayout(layout); // will btn be retroactively made a child of dlg here?
    

    https://doc.qt.io/qt-6/layout.html mentions that

    Widgets can only have other widgets as parent, not layouts.
    

    If so, how would retroactive parenting even work?
    Already searched the documentation as well as the forums and even google, but couldn't find any answer to this. So I'm asking it here since I'm worried it might create huge memory leaks when my dialog won't clean up the widgets in it once it goes out of scope.

    JonBJ 1 Reply Last reply
    0
    • E Estelyen

      I know that adding a Widget (let's say a QPushButton) to a QLayout will automatically set its parent to the Widget the layout is installed in (let's say a QDialog):

      QDialog* dlg { new QDialog() };
      QVBoxLayout* layout { new QVBoxLayout(dlg) };
      QPushButton* btn { new QPushButton("Click me!") };
      layout->addWidget(btn); // dlg is set as parent of btn here
      

      But if the layout is created parentless, will it retroactively assign parentage once I add it to the Dialog?

      QVBoxLayout* layout { new QVBoxLayout() }; // note: Not assigned to any widget yet
      QPushButton* btn { new QPushButton("Click me!") };
      layout->addWidget(btn); // btn cannot be assigned a parent
      ...
      QDialog* dlg { new QDialog() };
      dlg->setLayout(layout); // will btn be retroactively made a child of dlg here?
      

      https://doc.qt.io/qt-6/layout.html mentions that

      Widgets can only have other widgets as parent, not layouts.
      

      If so, how would retroactive parenting even work?
      Already searched the documentation as well as the forums and even google, but couldn't find any answer to this. So I'm asking it here since I'm worried it might create huge memory leaks when my dialog won't clean up the widgets in it once it goes out of scope.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote last edited by
      #2

      @Estelyen said in QLayout retroactive parenting?:

      dlg->setLayout(layout); // will btn be retroactively made a child of dlg here?

      So far as I know, yes.

      In any case, it's so easy to check e.g. btn->parent() after this, so if you're worried why not do so?

      E 1 Reply Last reply
      1
      • JonBJ JonB

        @Estelyen said in QLayout retroactive parenting?:

        dlg->setLayout(layout); // will btn be retroactively made a child of dlg here?

        So far as I know, yes.

        In any case, it's so easy to check e.g. btn->parent() after this, so if you're worried why not do so?

        E Offline
        E Offline
        Estelyen
        wrote last edited by
        #3

        @JonB said in QLayout retroactive parenting?:

        @Estelyen said in QLayout retroactive parenting?:

        dlg->setLayout(layout); // will btn be retroactively made a child of dlg here?

        So far as I know, yes.

        In any case, it's so easy to check e.g. btn->parent() after this, so if you're worried why not do so?

        Okay, facepalm moment for me, I searched fo so long and never tried checking it myself. Need some sleep, apparently.
        Anyways, I just did a test and it indeed assigned dlg as parent of btn. At least the memory addresses are the same. Thanks for the help!

        1 Reply Last reply
        1
        • E Estelyen has marked this topic as solved

        • Login

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