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. Delete a QString From QStringList

Delete a QString From QStringList

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 8 Posters 11.1k 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.
  • Z Offline
    Z Offline
    Zunneh
    wrote on 23 Jan 2020, 11:13 last edited by
    #1

    Hello everybody,
    imagine i have a QStringList which countains names of persons and i want to delete some names from this QStringList, how can i do it ? i tried
    CodeCountry = "FR";

               QStringList ListName; 
               for (int index =0;index < ListName.length() ; index ++)  {
                       if (ListName[index] == "Jon") || (ListName[index] == "Adam"){
                                  ListName.removeAll(ListName[index]);
               }
    

    but it didn't work, can i get help ? thanks

    my english is average, please use simple words and try to be the most explicit, thank you

    K J 2 Replies Last reply 23 Jan 2020, 11:21
    0
    • Z Zunneh
      23 Jan 2020, 11:13

      Hello everybody,
      imagine i have a QStringList which countains names of persons and i want to delete some names from this QStringList, how can i do it ? i tried
      CodeCountry = "FR";

                 QStringList ListName; 
                 for (int index =0;index < ListName.length() ; index ++)  {
                         if (ListName[index] == "Jon") || (ListName[index] == "Adam"){
                                    ListName.removeAll(ListName[index]);
                 }
      

      but it didn't work, can i get help ? thanks

      K Offline
      K Offline
      KroMignon
      wrote on 23 Jan 2020, 11:21 last edited by
      #2

      @Zunneh said in Delete a QString From QStringList:

      but it didn't work, can i get help ? thanks

      Why you don't simply do this?

      ListName.removeAll("Jon");
      ListName.removeAll("Adam");
      
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      Z 1 Reply Last reply 23 Jan 2020, 14:36
      10
      • Z Zunneh
        23 Jan 2020, 11:13

        Hello everybody,
        imagine i have a QStringList which countains names of persons and i want to delete some names from this QStringList, how can i do it ? i tried
        CodeCountry = "FR";

                   QStringList ListName; 
                   for (int index =0;index < ListName.length() ; index ++)  {
                           if (ListName[index] == "Jon") || (ListName[index] == "Adam"){
                                      ListName.removeAll(ListName[index]);
                   }
        

        but it didn't work, can i get help ? thanks

        J Offline
        J Offline
        JonB
        wrote on 23 Jan 2020, 12:08 last edited by JonB
        #3

        @Zunneh
        You must not remove elements from the list while still iterating through it with the code you show.

        Do as @KroMignon says anyway.

        M 1 Reply Last reply 23 Jan 2020, 14:11
        6
        • J JonB
          23 Jan 2020, 12:08

          @Zunneh
          You must not remove elements from the list while still iterating through it with the code you show.

          Do as @KroMignon says anyway.

          M Offline
          M Offline
          mchinand
          wrote on 23 Jan 2020, 14:11 last edited by
          #4

          @KroMignon's solution is best but if you have to do something similar with a container that doesn't have a convenient function to do it automatically, you could iterator through it in reverse, starting with the last element.

          J 1 Reply Last reply 23 Jan 2020, 14:23
          1
          • M mchinand
            23 Jan 2020, 14:11

            @KroMignon's solution is best but if you have to do something similar with a container that doesn't have a convenient function to do it automatically, you could iterator through it in reverse, starting with the last element.

            J Offline
            J Offline
            JonB
            wrote on 23 Jan 2020, 14:23 last edited by JonB
            #5

            @mchinand said in Delete a QString From QStringList:

            you could iterator through it in reverse, starting with the last element.

            Not in this case, given the code as written (which is why I phrased it that way). Even if going in reverse:

                       QStringList ListName; 
                       for (int index = ListName.length() - 1; index >= 0 ; index --)  {
                               if (ListName[index] == "Jon") || (ListName[index] == "Adam"){
                                          ListName.removeAll(ListName[index]);
                       }
            

            If this matches more than one line for any given name (I assume it might, else why use removeAll()), the list will be shortened by more than one element. When you then do the index-- for the next iteration in the for, you could then have an index which is now beyond the new ListName.length(), and ListName[index] will then "crash" (or at minimum revisit already visited elements).

            Your proposal would work for QList::removeAt(index), but not (safely) for QList::removeAll(list[index]).

            M 1 Reply Last reply 23 Jan 2020, 14:29
            2
            • J JonB
              23 Jan 2020, 14:23

              @mchinand said in Delete a QString From QStringList:

              you could iterator through it in reverse, starting with the last element.

              Not in this case, given the code as written (which is why I phrased it that way). Even if going in reverse:

                         QStringList ListName; 
                         for (int index = ListName.length() - 1; index >= 0 ; index --)  {
                                 if (ListName[index] == "Jon") || (ListName[index] == "Adam"){
                                            ListName.removeAll(ListName[index]);
                         }
              

              If this matches more than one line for any given name (I assume it might, else why use removeAll()), the list will be shortened by more than one element. When you then do the index-- for the next iteration in the for, you could then have an index which is now beyond the new ListName.length(), and ListName[index] will then "crash" (or at minimum revisit already visited elements).

              Your proposal would work for QList::removeAt(index), but not (safely) for QList::removeAll(list[index]).

              M Offline
              M Offline
              mchinand
              wrote on 23 Jan 2020, 14:29 last edited by
              #6

              @JonB Right, I didn't read the OP's code closely enough.

              1 Reply Last reply
              2
              • K KroMignon
                23 Jan 2020, 11:21

                @Zunneh said in Delete a QString From QStringList:

                but it didn't work, can i get help ? thanks

                Why you don't simply do this?

                ListName.removeAll("Jon");
                ListName.removeAll("Adam");
                
                
                Z Offline
                Z Offline
                Zunneh
                wrote on 23 Jan 2020, 14:36 last edited by
                #7

                @KroMignon the example i give was bad

                this is the real example, removing names from a QStringList who finish with the letter ' e ' ( if NameList[index].endswith('e') , so there are many names that finish with' e ', removeAll is applied when you know the names, in my case i don't know the Names, the QStringList will append different Names
                did you understand ?

                my english is average, please use simple words and try to be the most explicit, thank you

                J K 2 Replies Last reply 23 Jan 2020, 14:50
                0
                • Z Zunneh
                  23 Jan 2020, 14:36

                  @KroMignon the example i give was bad

                  this is the real example, removing names from a QStringList who finish with the letter ' e ' ( if NameList[index].endswith('e') , so there are many names that finish with' e ', removeAll is applied when you know the names, in my case i don't know the Names, the QStringList will append different Names
                  did you understand ?

                  J Offline
                  J Offline
                  JonB
                  wrote on 23 Jan 2020, 14:50 last edited by
                  #8

                  @Zunneh
                  In that case, do use the "reverse iteration" (i.e. counting downward) suggested by @mchinand and as per the for loop line I have written above. So long as the remove you have in the if inside it will only remove one row ( if (whatever) ListName.removeAt(index)) then it is good.

                  1 Reply Last reply
                  1
                  • Z Zunneh
                    23 Jan 2020, 14:36

                    @KroMignon the example i give was bad

                    this is the real example, removing names from a QStringList who finish with the letter ' e ' ( if NameList[index].endswith('e') , so there are many names that finish with' e ', removeAll is applied when you know the names, in my case i don't know the Names, the QStringList will append different Names
                    did you understand ?

                    K Offline
                    K Offline
                    KroMignon
                    wrote on 23 Jan 2020, 14:53 last edited by
                    #9

                    @Zunneh said in Delete a QString From QStringList:

                    this is the real example, removing names from a QStringList who finish with the letter ' e ' ( if NameList[index].endswith('e') , so there are many names that finish with' e ', removeAll is applied when you know the names, in my case i don't know the Names, the QStringList will append different Names
                    did you understand ?

                    That is not what you give as implementation!
                    One possible solution could be using QStringList::filter():

                    const auto toRemove = ListName.filter(QRegularExpression(".*e"));
                    for(const auto &item : toRemove)
                        ListName.removeAll(item);
                    

                    or

                    const QRegularExpression filter(".*e");
                    for(int idx = 0; idx < ListName.size(); ++idx)
                    {
                        if(ListName.at(idx).contains(filter))
                        {
                            ListName.remove(idx);
                            --idx;
                        }
                    }
                    

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    Z 1 Reply Last reply 23 Jan 2020, 14:55
                    4
                    • K KroMignon
                      23 Jan 2020, 14:53

                      @Zunneh said in Delete a QString From QStringList:

                      this is the real example, removing names from a QStringList who finish with the letter ' e ' ( if NameList[index].endswith('e') , so there are many names that finish with' e ', removeAll is applied when you know the names, in my case i don't know the Names, the QStringList will append different Names
                      did you understand ?

                      That is not what you give as implementation!
                      One possible solution could be using QStringList::filter():

                      const auto toRemove = ListName.filter(QRegularExpression(".*e"));
                      for(const auto &item : toRemove)
                          ListName.removeAll(item);
                      

                      or

                      const QRegularExpression filter(".*e");
                      for(int idx = 0; idx < ListName.size(); ++idx)
                      {
                          if(ListName.at(idx).contains(filter))
                          {
                              ListName.remove(idx);
                              --idx;
                          }
                      }
                      
                      Z Offline
                      Z Offline
                      Zunneh
                      wrote on 23 Jan 2020, 14:55 last edited by Zunneh
                      #10

                      @KroMignon yeah after asking the question, i was in the kitchen and it comes on my mind to do indexx -- if he delete one item ( like the second solution )
                      on more question, can we put more than one filter ? for example delete name s who finish with 'e', 'y' and 'm' ??

                      @JonB i wil try your solution too

                      finosh wo

                      Thanks guys :)

                      my english is average, please use simple words and try to be the most explicit, thank you

                      J K 2 Replies Last reply 23 Jan 2020, 15:04
                      0
                      • Z Zunneh
                        23 Jan 2020, 14:55

                        @KroMignon yeah after asking the question, i was in the kitchen and it comes on my mind to do indexx -- if he delete one item ( like the second solution )
                        on more question, can we put more than one filter ? for example delete name s who finish with 'e', 'y' and 'm' ??

                        @JonB i wil try your solution too

                        finosh wo

                        Thanks guys :)

                        J Offline
                        J Offline
                        JonB
                        wrote on 23 Jan 2020, 15:04 last edited by JonB
                        #11

                        @Zunneh said in Delete a QString From QStringList:

                        on more question, can we put more than one filter ? for example delete name s who finish with 'e', 'y' and 'm' ??

                        I have not looked to see if accepted, but I would guess either of

                        const QRegularExpression filter(".*[eym]");
                        const QRegularExpression filter(".*(e|y|m)");
                        

                        (I assume you mean "finish with 'e', 'y' or 'm'"!). Regular expression [abc] means any one letter of, (ab|c|def) has to be used if multiple letter sequences required.

                        1 Reply Last reply
                        0
                        • Z Zunneh
                          23 Jan 2020, 14:55

                          @KroMignon yeah after asking the question, i was in the kitchen and it comes on my mind to do indexx -- if he delete one item ( like the second solution )
                          on more question, can we put more than one filter ? for example delete name s who finish with 'e', 'y' and 'm' ??

                          @JonB i wil try your solution too

                          finosh wo

                          Thanks guys :)

                          K Offline
                          K Offline
                          KroMignon
                          wrote on 23 Jan 2020, 15:11 last edited by
                          #12

                          @Zunneh said in Delete a QString From QStringList:

                          on more question, can we put more than one filter ? for example delete name s who finish with 'e', 'y' and 'm' ??

                          This is a regular expression, so it is up to you to set it up to match your needs.
                          For 'e', 'y' and 'm' it would be: const QRegularExpression filter(".*(e|y|m)");
                          Take a look at https://www.jrebel.com/sites/rebel/files/pdfs/regular-expressions-cheat-sheet.pdf

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          1 Reply Last reply
                          1
                          • V Offline
                            V Offline
                            VRonin
                            wrote on 23 Jan 2020, 15:53 last edited by VRonin
                            #13

                            stl-iterators are safe for calling erase on. The Qt functionality is identical to the one of std::vector.
                            Given a generic function bool shouldIDeleteThisString(const QString&) that returns true if the string should be removed from the list you can use:

                            for(auto i = list.begin(); i!=list.end();){
                                if(shouldIDeleteThisString(*i))
                                    i=list.erase(i);
                                else
                                    ++i;
                            }
                            

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            C 1 Reply Last reply 23 Jan 2020, 16:53
                            4
                            • V VRonin
                              23 Jan 2020, 15:53

                              stl-iterators are safe for calling erase on. The Qt functionality is identical to the one of std::vector.
                              Given a generic function bool shouldIDeleteThisString(const QString&) that returns true if the string should be removed from the list you can use:

                              for(auto i = list.begin(); i!=list.end();){
                                  if(shouldIDeleteThisString(*i))
                                      i=list.erase(i);
                                  else
                                      ++i;
                              }
                              
                              C Offline
                              C Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 23 Jan 2020, 16:53 last edited by
                              #14

                              @VRonin since you're doing iterator fun:

                              list.erase(std::remove_if(list.begin(), list.end(), shouldIDeleteThisString), list.end());
                              

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

                              J 1 Reply Last reply 23 Jan 2020, 17:22
                              7
                              • C Christian Ehrlicher
                                23 Jan 2020, 16:53

                                @VRonin since you're doing iterator fun:

                                list.erase(std::remove_if(list.begin(), list.end(), shouldIDeleteThisString), list.end());
                                
                                J Offline
                                J Offline
                                JonB
                                wrote on 23 Jan 2020, 17:22 last edited by
                                #15

                                @Christian-Ehrlicher
                                Indeed, because I looked it up, but it doesn't make it readable, or the way it works very intelligible, IMHO! :)

                                aha_1980A 1 Reply Last reply 23 Jan 2020, 20:36
                                0
                                • J JonB
                                  23 Jan 2020, 17:22

                                  @Christian-Ehrlicher
                                  Indeed, because I looked it up, but it doesn't make it readable, or the way it works very intelligible, IMHO! :)

                                  aha_1980A Offline
                                  aha_1980A Offline
                                  aha_1980
                                  Lifetime Qt Champion
                                  wrote on 23 Jan 2020, 20:36 last edited by
                                  #16

                                  @JonB

                                  but it doesn't make it readable

                                  Get used to it, that's the preferred C++ way nowadays ("functional programming").

                                  Regards

                                  Qt has to stay free or it will die.

                                  kshegunovK 1 Reply Last reply 24 Jan 2020, 21:29
                                  1
                                  • aha_1980A aha_1980
                                    23 Jan 2020, 20:36

                                    @JonB

                                    but it doesn't make it readable

                                    Get used to it, that's the preferred C++ way nowadays ("functional programming").

                                    Regards

                                    kshegunovK Offline
                                    kshegunovK Offline
                                    kshegunov
                                    Moderators
                                    wrote on 24 Jan 2020, 21:29 last edited by
                                    #17

                                    @aha_1980 said in Delete a QString From QStringList:

                                    Get used to it, that's the preferred C++ way nowadays ("functional programming").

                                    The major reason for the mortality rise in the developers demographics ... jumping off a tall building ain't no fun ...

                                    Read and abide by the Qt Code of Conduct

                                    1 Reply Last reply
                                    3

                                    8/17

                                    23 Jan 2020, 14:50

                                    • Login

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