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.
  • M mchinand

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

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on 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
    2
    • JonBJ JonB

      @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 last edited by
      #6

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

      1 Reply Last reply
      2
      • KroMignonK KroMignon

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

        JonBJ KroMignonK 2 Replies Last reply
        0
        • Z Zunneh

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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

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

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on 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
            4
            • KroMignonK KroMignon

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

              JonBJ KroMignonK 2 Replies Last reply
              0
              • Z Zunneh

                @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 :)

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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

                  @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 :)

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on 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
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on 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

                    Christian EhrlicherC 1 Reply Last reply
                    4
                    • VRoninV VRonin

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

                      JonBJ 1 Reply Last reply
                      7
                      • Christian EhrlicherC Christian Ehrlicher

                        @VRonin since you're doing iterator fun:

                        list.erase(std::remove_if(list.begin(), list.end(), shouldIDeleteThisString), list.end());
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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
                        0
                        • JonBJ JonB

                          @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 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
                          1
                          • aha_1980A aha_1980

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

                            • Login

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