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.8k 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 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
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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

      ma_veasnaM 1 Reply Last reply
      1
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #5

        As SGalst suggest, please read the documentation carefully.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          butterface
          wrote on 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 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 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 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 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 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
                    • SGaistS SGaist

                      Hi,

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

                      ma_veasnaM Offline
                      ma_veasnaM Offline
                      ma_veasna
                      wrote on 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