Results 1 to 6 of 6

Thread: QStringlist contains() usage

  1. #1
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QStringlist contains() usage

    Can anyone correct me here ?

    Qt Code:
    1. list << "ABC" << "123";
    2. QString testStr = "AB"
    3. if (list.contains(testStr))
    To copy to clipboard, switch view to plain text mode 
    contains() ALWAYS returns false !
    Also,
    Qt Code:
    1. QRegExp rx(testStr+"*");
    2. rx.setPatternSyntax(QRegExp::Wildcard);
    3. if (list.indexOf(rx) != -1)
    To copy to clipboard, switch view to plain text mode 
    ALWAYS returns -1

    What am I missing ?
    Last edited by vieraci; 28th October 2009 at 12:18. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QStringlist contains() usage

    Hi,

    Because conatins is comparing "AB" with "ABC" and "123" and "AB" is different to "ABC" and different to "123".

    Maybe you want to try something like this:
    Qt Code:
    1. list << "ABC" << "123";
    2. bool bFound = false;
    3. int i = 0;
    4. while ((!bFound) && (i<list.count()))
    5. {
    6. if (list.at(i).contains(QString("AB"))
    7. bFound = true:
    8. else
    9. i++;
    10. }
    11. //Here you know the index(i) of the founded string
    To copy to clipboard, switch view to plain text mode 

    This code is not tested.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStringlist contains() usage

    Or a slightly faster version:

    Qt Code:
    1. bool listContains(const QStringList &list, const QString &str){
    2. QStringMatcher matcher(str);
    3. foreach(const QString &listitem, list){
    4. if(matcher.indexIn(listitem)!=-1) return true;
    5. }
    6. return false;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QStringlist contains() usage

    Hi,

    Didn't know about QStringMatcher class. Good apointment
    Òscar Llarch i Galán

  5. #5
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStringlist contains() usage

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    Didn't know about QStringMatcher class. Good apointment
    Neither did I !

    so, QStringList::contains() does not match partial strings huh ?

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QStringlist contains() usage

    Hi,

    Please read my first reply on this post. I've explained it there.
    Òscar Llarch i Galán

  7. The following user says thank you to ^NyAw^ for this useful post:

    vieraci (31st October 2009)

Similar Threads

  1. Very high CPU usage with QTableView and MVC
    By montylee in forum Qt Programming
    Replies: 7
    Last Post: 24th March 2009, 06:14
  2. QStringList
    By jaca in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2008, 10:12
  3. QStringList in QObject::connect
    By DPinLV in forum Qt Programming
    Replies: 6
    Last Post: 6th September 2006, 17:01
  4. Cannot queue arguments of type 'QStringList'
    By vfernandez in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2006, 20:48
  5. need help to classify some QStringList
    By patcito in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 21:24

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
  •  
Qt is a trademark of The Qt Company.