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. Clarification on deleting layouts and widgets within them

Clarification on deleting layouts and widgets within them

Scheduled Pinned Locked Moved Solved General and Desktop
qlayoutitemqlayoutqwidgetruntimedelete
5 Posts 3 Posters 4.7k 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.
  • Sh1gsS Offline
    Sh1gsS Offline
    Sh1gs
    wrote on last edited by
    #1

    Hello Qt Universe,

    I would like some clarification and some help on something I'm working on. Let's say that I have the following:
    note: this wasn't copied from my code so please forgive syntax errors

    QPushButton *btn1, *btn2, *btn3, *btn4, *delBtn;
    QHBoxLayout *hbox1, *hbox2;
    QVBoxLayout *vbox1;
    
    btn1 = new QPushButton(Tr("1");
    btn2 = new QPushButton(Tr("2");
    btn3 = new QPushButton(Tr("3");
    btn4 = new QPushButton(Tr("4");
    delBtn = new QPushButton(Tr("del");
    
    connect(delBtn, &QPushButton::clicked, this, &MyClass::delBtnClicked);
    
    hbox1 = new QHBoxLayout;
    hbox1->addWidget(btn1);
    hbox1->addWidget(btn2);
    
    hbox2 = new QHBoxLayout;
    hbox2->addWidget(btn3);
    hbox2->addWidget(btn4);
    
    vbox1 = new QVBoxLayout;
    vbox1->addLayout(hbox1);
    vbox1->addLayout(hbox2);
    
    setLayout(vbox1);
    
    void MyClass::delBtnClicked()
    {
         //remove and delete entire vbox1 layout
    }
    

    I've seen the following:

    QLayoutItem *child;
    while ((child = layout->takeAt(0)) != 0) {
    ...
    delete child;
    }
    

    Would each hbox be "child" and "layout" would be vbox? And if I delete "child" will that then delete the QPushButton widgets within hbox1 and hbox2?

    Thanks!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Most likely yes.

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

      Sh1gsS 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Most likely yes.

        Sh1gsS Offline
        Sh1gsS Offline
        Sh1gs
        wrote on last edited by
        #3

        Well I got my example to work by using the following:

        while(!vbox1->isEmpty()) {
            QLayout *hb = vbox1->takeAt(0)->layout();
            while(!hb->isEmpty()) {
                QWidget *w = hb->takeAt(0)->widget();
                delete w;
            }
            delete hb;
        }
        delete vbox1;
        

        This removes the widgets and the layout they're on, as well as deletes them from memory. Thanks!

        mrjjM 1 Reply Last reply
        2
        • Sh1gsS Sh1gs

          Well I got my example to work by using the following:

          while(!vbox1->isEmpty()) {
              QLayout *hb = vbox1->takeAt(0)->layout();
              while(!hb->isEmpty()) {
                  QWidget *w = hb->takeAt(0)->widget();
                  delete w;
              }
              delete hb;
          }
          delete vbox1;
          

          This removes the widgets and the layout they're on, as well as deletes them from memory. Thanks!

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Sh1gs
          Yes Deleting QLayoutItem seems not to free any Widget it manages.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            No, it doesn't. The QLayoutItem is not the parent of the widget, it is not a QObject. That's not its job.

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

            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