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] Delete all Items from Layout
QtWS25 Last Chance

[solved] Delete all Items from Layout

Scheduled Pinned Locked Moved General and Desktop
deletelayoutall items
7 Posts 2 Posters 12.9k 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.
  • Q Offline
    Q Offline
    QT-static-prgm
    wrote on 1 Aug 2015, 12:14 last edited by QT-static-prgm 8 Jan 2015, 12:57
    #1

    I need your help again.
    I have an Dialog with an QVBoxLayout. I'll add QHBoxLayouts with Labels and Spinbox to the QVboxLayout with an function. How can i delete all Items when the dialog is deleted??
    In my case the Dialog's destructor deletes the QVBoxLayout. But all other layouts with the objects i added in the function will not be deleted. How can i do that?? I looked for an property to make the layout delete all items in the destructor, but i haven't found such an property.

    M 1 Reply Last reply 1 Aug 2015, 12:28
    0
    • Q QT-static-prgm
      1 Aug 2015, 12:14

      I need your help again.
      I have an Dialog with an QVBoxLayout. I'll add QHBoxLayouts with Labels and Spinbox to the QVboxLayout with an function. How can i delete all Items when the dialog is deleted??
      In my case the Dialog's destructor deletes the QVBoxLayout. But all other layouts with the objects i added in the function will not be deleted. How can i do that?? I looked for an property to make the layout delete all items in the destructor, but i haven't found such an property.

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 1 Aug 2015, 12:28 last edited by mrjj 8 Jan 2015, 12:54
      #2

      @QT-static-prgm

      Have you tried to set the parent of the inserted widgets to the dialog ?
      (not sure it works)

      Alternatively you could kill em all

      #include<QLayoutItem>
      void remove ( QLayout* layout )
      {
          QLayoutItem* child;
          while ( layout->count() != 0 ) {
              child = layout->takeAt ( 0 );
              if ( child->layout() != 0 ) {
                  remove ( child->layout() );
              } else if ( child->widget() != 0 ) {
                  delete child->widget();
              }
      
              delete child;
          }
      }
      

      from dialog
      remove ( ui->gridLayout );

      Note:
      Im not sure its the best way to do it but it does show how to delete them all :)

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QT-static-prgm
        wrote on 1 Aug 2015, 12:35 last edited by
        #3

        I tried to add the widget as parent, but i'm not sure if it'll delete everything. How can i check that??

        M 1 Reply Last reply 1 Aug 2015, 12:37
        0
        • Q QT-static-prgm
          1 Aug 2015, 12:35

          I tried to add the widget as parent, but i'm not sure if it'll delete everything. How can i check that??

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 1 Aug 2015, 12:37 last edited by
          #4

          @QT-static-prgm
          Well make a new widget that inherited a button and place it in your layout,
          and place a break point in its destructor.

          How did you know before it did not get deleted?

          1 Reply Last reply
          1
          • Q Offline
            Q Offline
            QT-static-prgm
            wrote on 1 Aug 2015, 12:56 last edited by
            #5

            well i have this function:

            void SellHouseDialog::addStreet(QString name, int house)
            {
            	QHBoxLayout* hLayout = new QHBoxLayout(this);
            
            	// set Streetname
            	hLayout->addWidget(new QLabel(name, this));
            
            	// setup the spinbox
            	QSpinBox* box = new QSpinBox(this);
            	box->setRange(0, house);
            	box->setValue(house);
            	hLayout->addWidget(box);
            
            	ui->contentContainer->addLayout(hLayout);
            
            }
            

            and the documentation of the QVBoxLayout's destructor says
            "Destroys this box layout.

            The layout's widgets aren't destroyed."

            So i know that they were not deleted ;) I added the widget as parent, and run with the debugger through the whole destructor (much time later) the destructor from QLable and QSpinbox where called. So now i know that everything is cleaned up :D

            Thank you very much

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 1 Aug 2015, 13:01 last edited by
              #6

              Ok super
              so parent(ing) did work.

              I was not sure so good you report back so I learn new stuff too.

              Heh yeah, debugging the whole shutdown is many steps :)

              Have a nice evening

              Q 1 Reply Last reply 1 Aug 2015, 13:20
              0
              • M mrjj
                1 Aug 2015, 13:01

                Ok super
                so parent(ing) did work.

                I was not sure so good you report back so I learn new stuff too.

                Heh yeah, debugging the whole shutdown is many steps :)

                Have a nice evening

                Q Offline
                Q Offline
                QT-static-prgm
                wrote on 1 Aug 2015, 13:20 last edited by
                #7

                @mrjj

                yes debugging the whole destructor are many steps. My F11 key is still blazing.

                ähm and it's afternoon for me :D but thanks

                1 Reply Last reply
                0

                7/7

                1 Aug 2015, 13:20

                • Login

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