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 items from Layout
Forum Update on Monday, May 27th 2025

[Solved] Remove items from Layout

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 18.1k 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.
  • D Offline
    D Offline
    donitaroid
    wrote on 11 May 2012, 02:46 last edited by
    #1

    I wrote a program which receive the some test result from internet. The test result I received have to be displayed on the QWidget.

    Here is the design I have imagined: I can clear all items in the QVBoxLayout, and then fill the new content what I have received.

    I wrote some code according the logic above, and it does not work as what I thought. The executing screen shot:
    !http://img829.imageshack.us/img829/2040/removeitemfromlayout.png(::executing screen shot::)!

    It leaves some.... shadow or the items should be already remove all sqeezed in the first row.

    Is there any problem with my code?

    @

    int totalCount = ui->resultTextContainerLayout->count();
    for (int i = 0; i < totalCount; i++) {
        ui->resultTextContainerLayout->removeItem(ui->resultTextContainerLayout->itemAt(0));
    }
    //ui->resultTextContainerLayout->invalidate();
    
    for (int i = 0 ; i < eBoxDiskNum ; i++) {
        QLabel* tempLabel = new QLabel(ui->resultTextContainer);
        tempLabel->setFont(QFont("Calibri", 12));
        if (iErrCode & (1 << i)) {
            tempLabel->setStyleSheet("QLabel {color:red;}");
            tempLabel->setText(QString("Client%1 -> not ready").arg(i+1));
        } else {
            tempLabel->setStyleSheet("QLabel {color:black;}");
            tempLabel->setText(QString("Client%1 -> ready").arg(i+1));
        }
        ui->resultTextContainerLayout->addWidget(tempLabel);
    }
    ui->resultTextContainerLayout->addStretch();
    

    @

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on 11 May 2012, 06:30 last edited by
      #2

      QLayout::removeItem() just removes the item from layout, but does not hide or delete it.
      @
      QLayoutItem *item;
      while ((item = ui->resultTextContainerLayout->takeAt(0)) != 0) {
      delete item;
      }

      for (int i = 0 ; i < eBoxDiskNum ; i++) {
      ...
      @

      Be aware that the above code only works for non-nested layouts (layouts only containing widgets). If you add nested layouts to your layout you will need a recursive removal. See "this":http://qt-project.org/forums/viewthread/13583/#71685 thread for further information.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        donitaroid
        wrote on 11 May 2012, 08:25 last edited by
        #3

        Yes, it works.

        There are some difference between the code I tested which is I delete it using widget()

        @
        QLayoutItem *item = NULL;
        while ((item = ui->resultTextContainerLayout->takeAt(0)) != 0) {
        delete item->widget();
        }
        @

        Thank you very much

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on 11 May 2012, 10:21 last edited by
          #4

          Be aware that not every item must be a widget! Your code actually works because <code>delete 0</code> is allowed in C++.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bird_alone
            wrote on 8 Jun 2014, 14:01 last edited by
            #5

            use this to clear all items inside a layout...

            @void clearLayout(QLayout *layout)
            {
            QLayoutItem *child;
            while ((child = layout->takeAt(0)) != 0) {
            if(child->layout() != 0)
            clearLayout( child->layout() );
            else if(child->widget() != 0)
            delete child->widget();

                delete child;
            }
            

            }@

            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