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 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 Offline
    C Offline
    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 Offline
        C Offline
        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 Offline
              C Offline
              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
                  • S Offline
                    S 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

                      15/15

                      4 May 2022, 08:48

                      • Login

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