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.
  • S Offline
    S Offline
    Sh1gs
    wrote on 8 Mar 2017, 17:22 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 8 Mar 2017, 22:20 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

      S 1 Reply Last reply 10 Mar 2017, 16:04
      1
      • S SGaist
        8 Mar 2017, 22:20

        Hi,

        Most likely yes.

        S Offline
        S Offline
        Sh1gs
        wrote on 10 Mar 2017, 16:04 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!

        M 1 Reply Last reply 10 Mar 2017, 16:10
        2
        • S Sh1gs
          10 Mar 2017, 16:04

          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!

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 10 Mar 2017, 16:10 last edited by
          #4

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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 10 Mar 2017, 22:17 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

            1/5

            8 Mar 2017, 17:22

            • 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