Results 1 to 2 of 2

Thread: QTextEdit - Finding text within selection

  1. #1
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTextEdit - Finding text within selection

    Hello,

    I am creating a "find" dialog for my text editor. I have the basic functionality working but I'm not having any luck with limiting the search to a selection only (this is a great feature in many code editors).

    Here's where I am so far (This is the slot):
    Qt Code:
    1. void MainWindow::findString(const QString str, int flags, bool selectionOnly)
    2. {
    3. QTextDocument::FindFlags ff = (QTextDocument::FindFlags)flags;
    4. if(selectionOnly)
    5. <Code for finding within selection goes here>
    6. else
    7. currentEditor->find(str,ff); // currentEditor is a QTextEdit
    8. }
    To copy to clipboard, switch view to plain text mode 

    What would be the most efficient way to limit the search to only the selected text?

    Any help is appreciated.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextEdit - Finding text within selection

    <warning type="top of head" />

    How about something like this:
    • Grab the QTextEdit::textCursor()
    • If no selection then return?
    • Position newcursor at start of selection
    • Execute newcursor = find(str, newcursor, opts)
    • If newcursor still inside selection then report match and repeat find(). You have operator<() to compare cursors, or you can use the start or end positions manually.

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

    kringen (10th June 2012)

Similar Threads

  1. Replies: 4
    Last Post: 11th September 2011, 01:39
  2. Problem with Finding the Text
    By charlesprime in forum Qt Programming
    Replies: 4
    Last Post: 24th March 2011, 10:10
  3. Problem finding text in a QTextEdit
    By Vash5556 in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2009, 14:19
  4. Multiple selection in QTextEdit
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 1st July 2006, 07:58
  5. Finding text on Text edit
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 18th May 2006, 13:20

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.