Results 1 to 7 of 7

Thread: Finding whole word in QString ?

  1. #1
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Finding whole word in QString ?

    How to check whole word in QString ?
    In Find Files example given in Qt assistance , How to add Match whole word setting.

    Qt Code:
    1. QFile file(directory.absoluteFilePath(files[i]));
    2.  
    3. if (file.open(QIODevice::ReadOnly)) {
    4. QString line;
    5. QTextStream in(&file);
    6. while (!in.atEnd()) {
    7. if (progressDialog.wasCanceled())
    8. break;
    9. line = in.readLine();
    10. if (line.contains(text)) -->How to check whole word in QString ?:confused:
    11. {
    12. foundFiles << files[i];
    13. break;
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 



    Must Watch: See how we can connect digital world objects to our day to day life….

  2. #2
    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: Finding whole word in QString ?

    Look at using QRegExp.

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Finding whole word in QString ?

    doesn't QString::contains work for you?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Finding whole word in QString ?

    QString::contains dont check "whole word " condition.

  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: Finding whole word in QString ?

    Quote Originally Posted by rajesh View Post
    QString::contains dont check "whole word " condition.
    QRegExp does, take a look.

  6. #6
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Finding whole word in QString ?

    I didn't get any examples of QRegEdit to solve my problem.
    how to use
    bool QRegExp::exactMatch ( const QString & str ) const

    if I use QRegExp then if user uncheck regularExp box then how to use QRegExp.
    see the attach dialog.



    my problem is solved with following code:
    Qt Code:
    1. QTextDocument document(line);
    2. cursor = regularExp ? document.find(findText, cursor, myFlags):document.find(text, cursor, myFlags);
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  7. #7
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Finding whole word in QString ?

    QRegExp with "\b"+searchStr+"\b" as pattern should do the trick. \b searches for word boundaries.

    HIH

    Johannes

Similar Threads

  1. QString find Whole word Only
    By bcteh_98 in forum Qt Programming
    Replies: 9
    Last Post: 7th December 2018, 08:47
  2. Qy 4.4.3 MySQL driver failed
    By pamalite in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd January 2010, 01:09
  3. File rename detection
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 15:22
  4. How to fhind whole word only in QString
    By sawerset in forum Qt Programming
    Replies: 2
    Last Post: 12th December 2008, 05:18
  5. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

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.