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: Remove widgets from Layout
Forum Updated to NodeBB v4.3 + New Features

SOLVED: Remove widgets from Layout

Scheduled Pinned Locked Moved General and Desktop
12 Posts 6 Posters 61.6k Views 1 Watching
  • 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
    Qatto
    wrote on 30 Jul 2013, 18:54 last edited by
    #1

    Hello,
    How do I remove all widgets from a layout so as to leave it empty
    @
    QPushButton *button1 = new QPushButton("Button 1");
    QPushButton *button2 = new QPushButton("Button 2");
    ui->horizontalLayout->addWidget(button1);
    ui->horizontalLayout->addWidget(button2);
    @

    How do I empty ui->horizontalLayout?
    Thanks in Advance

    Web/Desktop Developer

    1 Reply Last reply
    0
    • P Offline
      P Offline
      podsvirov
      wrote on 30 Jul 2013, 19:22 last edited by
      #2

      I do not know of a method to remove all widgets at once, but if you have
      pointers to the widgets, you can remove them from the layout like this:
      @
      ui->horizontalLayout->removeWidget(button1);
      ui->horizontalLayout->removeWidget(button2);
      @

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qatto
        wrote on 30 Jul 2013, 20:22 last edited by
        #3

        That would work, But I need to clear all the widgets in the first statement of the function. In otherwords, the widgets declared in the previous call are removed when the function is called. So at that point you can't use removeWidget()

        Web/Desktop Developer

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 30 Jul 2013, 20:25 last edited by
          #4

          Hi,

          You're looking for "takeAt":http://qt-project.org/doc/qt-4.8/qlayout.html#takeAt

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

          M 1 Reply Last reply 12 Mar 2020, 06:14
          1
          • D Offline
            D Offline
            dbzhang800
            wrote on 31 Jul 2013, 01:20 last edited by
            #5

            As SGalst suggest, please read the documentation carefully.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              butterface
              wrote on 31 Jul 2013, 08:23 last edited by
              #6

              I am doing it this way normally

              @
              QLayoutItem *wItem;
              if (!ui->widget->layout())
              ui->widget->setLayout(new QVBoxLayout());
              while ((wItem = ui->widget->layout()->takeAt(0)) != 0)
              {
              if (wItem->widget())
              wItem->widget()->setParent(NULL);
              delete wItem;
              }
              @

              Of course you can avoid the generation of the layout and the remove everything from the empty layout...

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                Qatto
                wrote on 31 Jul 2013, 16:07 last edited by
                #7

                Supposing the contents of the Layout are widgets added with layout->addWidget(). How do I remove them? The methods above remove items of type QLayoutItem only

                Web/Desktop Developer

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  butterface
                  wrote on 31 Jul 2013, 16:53 last edited by
                  #8

                  That's what the documentation for QLayoutItem says:

                  bq.
                  isEmpty() returns whether the layout item is empty. If the concrete item is a QWidget, it can be retrieved using widget(). Similarly for layout() and spacerItem().

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    Qatto
                    wrote on 31 Jul 2013, 17:35 last edited by
                    #9

                    Thanks butterface, But am just a Qt beginner. Could you please show me how to do it in case its a widget that I want to remove without using removeWidget().

                    Code
                    @QLayoutItem *child; //replace with widget
                    while ((child = ui->HLayout->takeAt(0)) != 0) {
                    delete child;
                    }@

                    Web/Desktop Developer

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      butterface
                      wrote on 31 Jul 2013, 22:21 last edited by
                      #10

                      Without any testing and just the first guess this should look somehow like

                      @
                      QWidget *wItem;
                      while ((wItem = ui->widget->widget()->takeAt(0)) != 0)
                      {
                      wItem->setParent(NULL);
                      delete wItem;
                      }
                      @

                      If this does not work please have a look at the documentation and come back here if that does not help.

                      1 Reply Last reply
                      0
                      • Q Offline
                        Q Offline
                        Qatto
                        wrote on 1 Aug 2013, 08:15 last edited by
                        #11

                        Well, Thanks guys. I ended up using a widget container and did this:
                        @QList<QWidget *> Widgets = ui->widget->findChildren<QWidget *>();
                        foreach(QWidget * child, Widgets)
                        {
                        delete child;
                        }@

                        But now there is another problem: "Application crashes when removing tableWidget":http://qt-project.org/forums/viewthread/30727/

                        Web/Desktop Developer

                        1 Reply Last reply
                        0
                        • S SGaist
                          30 Jul 2013, 20:25

                          Hi,

                          You're looking for "takeAt":http://qt-project.org/doc/qt-4.8/qlayout.html#takeAt

                          M Offline
                          M Offline
                          ma_veasna
                          wrote on 12 Mar 2020, 06:14 last edited by
                          #12

                          @SGaist thank you I really like your answer which helped me . it's short but pretty useful.

                          1 Reply Last reply
                          1

                          • Login

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