Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. [How to delete a QList]

[How to delete a QList]

Scheduled Pinned Locked Moved Solved Qt 6
15 Posts 5 Posters 4.3k 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.
  • A Offline
    A Offline
    appdev
    wrote on 28 Apr 2022, 08:35 last edited by appdev
    #1

    Hello guys, I hope you're doing very well.

    So I have a QList which includes 9 elements which type is QString and I want to delete it.

    Some people suggested doing this:

    qDeleteAll(list);
    
    list.clear();
    

    So I tried it and got an error.

    That solution was suggested to someone who wanted to delete a QList of pointers to objects.

    So I was wondering how I can delete the whole list and not only its items.

    I've searched on the internet and all I found was how to delete the list's items.

    I also took a look on the documentation of Qt6 and didn't find any function that could ensure the Qlist deletion.

    Please Help me.

    Any little help would be so appreciated.

    Thank you all.

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 28 Apr 2022, 08:38 last edited by Christian Ehrlicher
      #2

      @appdev said in [How to delete a QList]:

      Some people suggested doing this:
      qDeleteAll(list);

      Only when you store pointers to objects which you don't when you use QList<QString>

      list.clear();

      That removes all elements from the list and is what you want.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • A Offline
        A Offline
        appdev
        wrote on 28 Apr 2022, 08:42 last edited by appdev
        #3

        @Christian-Ehrlicher First of all thank you.

        Well I used only list.clear() but I got an empty QList.

        In fact here is the real thing;

        I'm reading a csv file and storing all the lines in QList.

        So I have about 54000 QLists since the file includes 54000 lines.

        But my goal is to delete some lines that I won't be needing later in my program.

        I thought after storing the lines in QLists, I would only keep the QLists that correponsd to these lines so I had to delete them completely.

        After using list.clear() and using qDebug to see the output;

        I get the lists i need and other lists that are empty.

        C 1 Reply Last reply 28 Apr 2022, 08:49
        0
        • A appdev
          28 Apr 2022, 08:42

          @Christian-Ehrlicher First of all thank you.

          Well I used only list.clear() but I got an empty QList.

          In fact here is the real thing;

          I'm reading a csv file and storing all the lines in QList.

          So I have about 54000 QLists since the file includes 54000 lines.

          But my goal is to delete some lines that I won't be needing later in my program.

          I thought after storing the lines in QLists, I would only keep the QLists that correponsd to these lines so I had to delete them completely.

          After using list.clear() and using qDebug to see the output;

          I get the lists i need and other lists that are empty.

          C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 28 Apr 2022, 08:49 last edited by
          #4

          @appdev said in [How to delete a QList]:

          I get the lists i need and other lists that are empty.

          Then don't use clear but only remove the indexes you don't need or copy all needed into a new container and clear the old one.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • A Offline
            A Offline
            appdev
            wrote on 28 Apr 2022, 09:01 last edited by
            #5

            I think the remove functions, only remove the item in the QList and not the QList itself.

            Please correct me if I'm wrong.

            C 1 Reply Last reply 28 Apr 2022, 09:06
            0
            • A appdev
              28 Apr 2022, 09:01

              I think the remove functions, only remove the item in the QList and not the QList itself.

              Please correct me if I'm wrong.

              C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 28 Apr 2022, 09:06 last edited by
              #6

              @appdev said in [How to delete a QList]:

              I think the remove functions, only remove the item in the QList and not the QList itself.

              ?

              You said you've a QList<QList<Item>> - so why should removeAt() on the outer container remove a single Item?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • A Offline
                A Offline
                appdev
                wrote on 28 Apr 2022, 09:43 last edited by
                #7

                yes, I mean I have many QLists, about 54000.

                QList [ value_1; value_2; value_3; value_4; value_5]
                QList [ value_1; value_2; value_3; value_4; value_5]
                QList [ value_1; value_2; value_3; value_4; value_5]
                QList [ value_1; value_2; value_3; value_4; value_5]
                QList [ value_1; value_2; value_3; value_4; value_5]...... until 54000.

                I want to delete all the QLists which values at the third position or index are different from 05 and I want to keep only those which values at that position are equal to 5.
                At the end I sohuld only have 24 QLists.

                1 Reply Last reply
                0
                • C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 28 Apr 2022, 10:03 last edited by
                  #8

                  And what's the problem - I already told you how to remove an element of a QList:
                  Iterate over your list (from the back) and remove every element you don't want
                  or
                  Iterator over your list and copy all elements you want to a new list.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    appdev
                    wrote on 28 Apr 2022, 10:50 last edited by
                    #9

                    Okay Now I see, it does make sense but for QList which includes another QList.

                    Earlier I said that I have QList<QList<Item>> but that's not actually the case.

                    I have a QList <QString>.

                    So when I use the clear function, it deletes all of the QString elements.

                    When I use the QDebug to see if the list is gone or not, As an output I get an empty list.

                    But logically if I delete the list and use Qdebug(), I'm not supposed to see anything in the output of the application, not even an empty list.

                    I think there is a difference between deleting a list and deleting its items right ?

                    J C J 3 Replies Last reply 28 Apr 2022, 10:51
                    0
                    • A appdev
                      28 Apr 2022, 10:50

                      Okay Now I see, it does make sense but for QList which includes another QList.

                      Earlier I said that I have QList<QList<Item>> but that's not actually the case.

                      I have a QList <QString>.

                      So when I use the clear function, it deletes all of the QString elements.

                      When I use the QDebug to see if the list is gone or not, As an output I get an empty list.

                      But logically if I delete the list and use Qdebug(), I'm not supposed to see anything in the output of the application, not even an empty list.

                      I think there is a difference between deleting a list and deleting its items right ?

                      J Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 28 Apr 2022, 10:51 last edited by
                      #10

                      @appdev can you please show your actual code ?


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      0
                      • A appdev
                        28 Apr 2022, 10:50

                        Okay Now I see, it does make sense but for QList which includes another QList.

                        Earlier I said that I have QList<QList<Item>> but that's not actually the case.

                        I have a QList <QString>.

                        So when I use the clear function, it deletes all of the QString elements.

                        When I use the QDebug to see if the list is gone or not, As an output I get an empty list.

                        But logically if I delete the list and use Qdebug(), I'm not supposed to see anything in the output of the application, not even an empty list.

                        I think there is a difference between deleting a list and deleting its items right ?

                        C Online
                        C Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 28 Apr 2022, 10:54 last edited by
                        #11

                        @appdev said in [How to delete a QList]:

                        I have a QList <QString>.

                        So I have about 54000 QLists since the file includes 54000 lines.

                        What now?

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0
                        • A appdev
                          28 Apr 2022, 10:50

                          Okay Now I see, it does make sense but for QList which includes another QList.

                          Earlier I said that I have QList<QList<Item>> but that's not actually the case.

                          I have a QList <QString>.

                          So when I use the clear function, it deletes all of the QString elements.

                          When I use the QDebug to see if the list is gone or not, As an output I get an empty list.

                          But logically if I delete the list and use Qdebug(), I'm not supposed to see anything in the output of the application, not even an empty list.

                          I think there is a difference between deleting a list and deleting its items right ?

                          J Offline
                          J Offline
                          JonB
                          wrote on 28 Apr 2022, 11:16 last edited by JonB
                          #12

                          @appdev said in [How to delete a QList]:

                          But logically if I delete the list and use Qdebug(), I'm not supposed to see anything in the output of the application, not even an empty list.

                          Not true.

                          • If you have a QList variable, you will always have a list, which may be empty or have items. But you still have a QList.

                          • If instead you go for a QList * variable, which you new, you can then delete theQList when you're done with it, and you won't have an empty list, you will have no list. (You will still have a QList * variable, but it won't point to an (allocated) QList.)

                          This is not particular to QList, it applies to C++ generally.

                          Note, btw, that this is not advocating using a QList * over a QList. A QList * is just 4/8 bytes (plus the allocation overhead when you new it), but an empty QList may be not much bigger than that anyway, so why bother with a pointer, allocating etc. etc. unless you have a specific need for that.

                          1 Reply Last reply
                          2
                          • A Offline
                            A Offline
                            appdev
                            wrote on 29 Apr 2022, 08:54 last edited by
                            #13

                            Hello guys,
                            I got it, thank you for your precious help, I really appreciate it.

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 29 Apr 2022, 18:21 last edited by
                              #14

                              Hi,

                              In addition to the good points made by my fellow, deleting a pointer to a QList does not delete the content of the list if they are also pointers.

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

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                appdev
                                wrote on 4 May 2022, 08:48 last edited by
                                #15

                                Hello again,
                                Thank you for all the precious information.

                                1 Reply Last reply
                                0

                                6/15

                                28 Apr 2022, 09:06

                                • Login

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