Results 1 to 6 of 6

Thread: How to remove duplicate enteries from QStringList.

  1. #1
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default How to remove duplicate enteries from QStringList.

    Hi all

    Working on Qt 4.4.2 on my intel mac machine.

    Pls tell How to remove duplicate enteries from QStringList.
    Always Believe in Urself
    Merry

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to remove duplicate enteries from QStringList.

    if you do not care about the order of the list:
    Qt Code:
    1. QStringList sl = ...;
    2. sl = sl.toSet().toList();
    To copy to clipboard, switch view to plain text mode 

    Maybe there are more efficient ways to do that.

    Or you could do
    Qt Code:
    1. for (int i=0; i<sl.count(); ++i)
    2. {
    3. forever { // remove all occurrences at greater indexes
    4. int p=sl.lastIndexOf(sl.at[i]);
    5. if (p==i) break;
    6. sl.removeAt(p);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    Untested, of course. Have, fun!

  3. The following user says thank you to caduel for this useful post:

    merry (11th December 2008)

  4. #3
    Join Date
    Sep 2013
    Posts
    10
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: How to remove duplicate enteries from QStringList.

    QStringList has a remove duplicates
    QStringList::removeDuplicates()

  5. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    855
    Thanks
    68
    Thanked 56 Times in 54 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to remove duplicate enteries from QStringList.

    Wow! Only 11 years to give us a solution.
    Òscar Llarch i Galán

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,011
    Thanks
    233
    Thanked 636 Times in 626 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to remove duplicate enteries from QStringList.

    Do you know how long I have been sitting here waiting for this answer? Now I can finally finish my project!
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    855
    Thanks
    68
    Thanked 56 Times in 54 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to remove duplicate enteries from QStringList.

    Yes! I can sleep now.
    Òscar Llarch i Galán

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.