PDA

View Full Version : QTextEdit - Finding text within selection



kringen
2nd June 2012, 06:05
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):

void MainWindow::findString(const QString str, int flags, bool selectionOnly)
{
QTextDocument::FindFlags ff = (QTextDocument::FindFlags)flags;
if(selectionOnly)
<Code for finding within selection goes here>
else
currentEditor->find(str,ff); // currentEditor is a QTextEdit
}

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

Any help is appreciated.

ChrisW67
2nd June 2012, 08:41
<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.