Results 1 to 6 of 6

Thread: QTextEdit find all

  1. #1
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QTextEdit find all

    Hi all,

    I'm trying to find a way find all occurrences of a specified string in a QTexedit document. The QTextedit::find() function only selects the first occurrence. For example consider the following text:

    "the dog the cat",

    If i search for string "the" I want to highlight both "the". I also tried using the QTextDocument::find() function and QRegexp with no luck.

    Anyone here that have any suggestion to solve my problem.

    /Nick

  2. #2
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit find all

    I'm really stuck with this problem, do the lack of replys means that's there is no easy way to do it or that its so simple so nobody reply for that reason

  3. #3
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextEdit find all

    I think you have to call find() from the beginning of the document until it returns false.
    At every hit you can insert html-code to highlight the search string.

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

    Default Re: QTextEdit find all

    Quote Originally Posted by boudie View Post
    At every hit you can insert html-code to highlight the search string.
    It's better to use QTextEdit::setExtraSelections() for that.
    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.


  5. #5
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit find all

    Thank you for your reply's, I will check both your suggestions....

  6. #6
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit find all

    Thanks alot for pointing me to the solution for my problem, here is the code I use, it somewhat slow but its good enough for me.

    Qt Code:
    1. void TextWidget::search(QString& str, bool matchCase)
    2. {
    3. QList<QTextEdit::ExtraSelection> extraSelections;
    4.  
    5. if(!ui->textEdit->isReadOnly())
    6. {
    7. ui->textEdit->moveCursor(QTextCursor::Start);
    8. QColor color = QColor(Qt::gray).lighter(130);
    9.  
    10. while(ui->textEdit->find(str))
    11. {
    12. QTextEdit::ExtraSelection extra;
    13. extra.format.setBackground(color);
    14.  
    15. extra.cursor = ui->textEdit->textCursor();
    16. extraSelections.append(extra);
    17. }
    18. }
    19.  
    20. ui->textEdit->setExtraSelections(extraSelections);
    21. }
    To copy to clipboard, switch view to plain text mode 

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

    for_hope (17th January 2010)

Similar Threads

  1. how can I find number of lines in QTextedit ?
    By newplayer in forum Newbie
    Replies: 10
    Last Post: 14th August 2008, 23:02
  2. QTextEdit->find()
    By derrickbj in forum Qt Programming
    Replies: 1
    Last Post: 12th June 2008, 01:24
  3. Help this guy find its way.
    By Dg!Mortal in forum Newbie
    Replies: 3
    Last Post: 12th April 2008, 18:31
  4. Replies: 1
    Last Post: 16th February 2007, 07:22
  5. QTextEdit::find() backward not working
    By sukanyarn in forum Qt Programming
    Replies: 1
    Last Post: 15th November 2006, 19:33

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.