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]
Forum Updated to NodeBB v4.3 + New Features

[How to delete a QList]

Scheduled Pinned Locked Moved Solved Qt 6
15 Posts 5 Posters 4.4k 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.
  • A Offline
    A Offline
    appdev
    wrote on 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
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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 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.

        Christian EhrlicherC 1 Reply Last reply
        0
        • A appdev

          @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.

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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 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.

            Christian EhrlicherC 1 Reply Last reply
            0
            • A appdev

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

              Please correct me if I'm wrong.

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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 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
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 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 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.HilkJ Christian EhrlicherC JonBJ 3 Replies Last reply
                    0
                    • A appdev

                      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.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on 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

                        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 ?

                        Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 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

                          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 ?

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on 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 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 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 last edited by
                                #15

                                Hello again,
                                Thank you for all the precious information.

                                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