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. Clearing QList fails

Clearing QList fails

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 2.2k 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.
  • B Offline
    B Offline
    Binary91
    wrote on last edited by
    #1

    Hi,
    I'm using a loop to clear and simultaneously delete a QList's contents (QTextEdits):
    @QMessageBox::information(0, "counter", "count before: "+QString::number(list.count()), QMessageBox::Ok);
    for(int i = 0; i < list.count(); i++)
    {
    delete list.takeLast();
    }
    QMessageBox::information(0, "counter", "count after: "+QString::number(list.count()), QMessageBox::Ok);@
    Output:
    [quote]count before: 6

    count after: 3[/quote]Can someone tell me why the loop does not remove all it's contents??

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You can use qDeleteAll() instead. Simple and fast.

      The reason why this code does not work is the following: you are taking last element out of the list, then iterate i, then take another, iterate, and so on until i < list count. So the result is that i is increasing while list.count() is decreasing. This will never clear the whole list. If you want to do it manually, you need to do something like this:
      @
      while (!list.isEmpty()) {
      delete list.takeLast();
      }
      @

      (Z(:^

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Binary91
        wrote on last edited by
        #3

        [quote]The reason why this code does not work is the following: you are taking last element out of the list, then iterate i, then take another, iterate, and so on until i < list count. So the result is that i is increasing while list.count() is decreasing. [/quote]Hehe yeah that was it, I detected the mistake just a few seconds ago by watching the loop about 5min :-D

        [quote]You can use qDeleteAll() instead. Simple and fast.[/quote]Hui, didn't know this functionallity yet. Great thing! I did not realize QtAlgorithms so far, very good, exactly what I need.

        Thank's for your fast support! :)

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Sure, you are welcome :-) Happy coding!

          (Z(:^

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Binary91
            wrote on last edited by
            #5

            EDIT:
            One more question: All those QTextEdits have been added to a layout before. I think I also have to remove them from the layout, right? The problem is, QVBoxLayout does not support a clear() – or removeAll() function, but I can’t call removeWidget(list.last()) anymore, when I deleted them all with one command (qDeleteAll())… How would you manage this?

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              QObjects delete themselves automatically, you do not need to do anything.

              If your use case is special and you really need to delete a widget/ layout yourself, just call delete or deleteLater() on it: it will automatically delete all children QObjects.

              So, most probably, all you need to do here is to run
              @
              myLayout->deleteLater();
              @

              This will delete the layout and all the items inside it on the next event loop sweep.

              (Z(:^

              1 Reply Last reply
              0
              • B Offline
                B Offline
                Binary91
                wrote on last edited by
                #7

                [quote]QObjects delete themselves automatically, you do not need to do anything[/quote]Ah, ok I thought this only happens when a parent-child relationship exists, so adding a QObject to a QLayout via "addWidget()" sets the Widget to child of QLayout? I always thought, the parent is still the window that contains the layout and setting the layout via window->setLayout() will make all QObjects in QLayout childs of the window?! Don't know, maybe I'm wrong and they are all childs of the layout itsself, so QLayout will recognize when it's childs are deleted...

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  Please check, I am not 100% sure myself right now.

                  (Z(:^

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Binary91
                    wrote on last edited by
                    #9

                    Qt's documentation to QLayout::addItem():
                    [quote]Note: The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it.[/quote]So yes, you were right :-)

                    Thank's again.

                    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