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.
  • QT-static-prgmQ Offline
    QT-static-prgmQ Offline
    QT-static-prgm
    wrote on last edited by QT-static-prgm
    #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.

    mrjjM 1 Reply Last reply
    0
    • QT-static-prgmQ QT-static-prgm

      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.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #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
      • QT-static-prgmQ Offline
        QT-static-prgmQ Offline
        QT-static-prgm
        wrote on 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??

        mrjjM 1 Reply Last reply
        0
        • QT-static-prgmQ QT-static-prgm

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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
          • QT-static-prgmQ Offline
            QT-static-prgmQ Offline
            QT-static-prgm
            wrote on 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
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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

              QT-static-prgmQ 1 Reply Last reply
              0
              • mrjjM mrjj

                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

                QT-static-prgmQ Offline
                QT-static-prgmQ Offline
                QT-static-prgm
                wrote on 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

                • Login

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