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. How to delete widgets from Qvboxlayout
QtWS25 Last Chance

How to delete widgets from Qvboxlayout

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.7k 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.
  • S Offline
    S Offline
    saravanavelu39
    wrote on 1 Aug 2014, 10:15 last edited by
    #1

    Hi friends,

    i am facing the one issue,,how to delete all widget from qvboxlayout in Qt,,After deleteing the widget i need to add some widget in the same Qvboxlayout,,now i delete the widget from Qvboxlayout,,and after that i add the new widget means Qvboxlayout take more spaces between the two widgets,, In below i added the code for deleting the widget from layout
    @QLayoutItem* item;
    while ( ( item = rightui->listLayout->takeAt( 0 ) ) != NULL )
    {
    if (item->widget()) {
    qDebug() << "item widget: " << item->widget();
    delete item->widget();
    }
    delete item;
    }@
    Please Give me a good solution for this,,,Thanks

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 1 Aug 2014, 13:47 last edited by
      #2

      Try this.
      @
      int count = vlayout.count();
      for(int i=0;i<count;i++) {

      QLayoutItem *item = vlayout.itemAt(I)
      vLayout.removeItem(item)
      delete item;
      

      }@

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • C Online
        C Online
        Chris Kawa
        Lifetime Qt Champion
        wrote on 1 Aug 2014, 15:39 last edited by
        #3

        [quote author="Dheerendra" date="1406900857"]Try this.
        @
        int count = vlayout.count();
        for(int i=0;i<count;i++) {

        QLayoutItem *item = vlayout.itemAt(I)
        vLayout.removeItem(item)
        delete item;
        

        }@[/quote]
        Ouch. No. Don't do that. When you delete item 0 item 1 becomes item 0. You advance to delete item 1 and leave item 0 where it was. When you arrive at (half+1) item the itemAt(i) refers beyond the children count and referencing it will crash.

        The proper way is to either iterate backwards, deleting the last item or, like the OP did, delete first item until there are no left. that's what "the doc suggests":http://qt-project.org/doc/qt-5/qlayout.html#takeAt

        You don't need to delete item and the widget separately. When you delete an item it will delete the widget.

        As for the problem - I don't quite understand. What do you mean the layout takes more space? Can you give a simple example?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 1 Aug 2014, 16:32 last edited by
          #4

          oops chris. This super goof-up from my side. Thank you Chris for pointing out.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0

          2/4

          1 Aug 2014, 13:47

          • Login

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