PDA

View Full Version : Problem in FINDING Previous Word



charlesprime
21st February 2011, 10:37
I am trying to implement the Findtext function in QTextEdit. I can find text using the FindNext function.
BUT when i am trying to find the Text using the FindPrevious function , am getting some problem.
Help me out!!!!!!


cursor->movePosition(QTextCursor::End);

while(!cursor->atStart())
{

//cursor->setCharFormat(format);
cursor->select(QTextCursor::WordUnderCursor);
QString s=cursor->selectedText();

if(s.contains(findlinededit->text(),Qt::CaseInsensitive))
{
.................//this is used to change the color of the text when it is found!!
}
cursor->movePosition(QTextCursor::WordLeft,QTextCursor::Ke epAnchor);

}//end of while

Even i tried using the QTextCursor::PreviousWord. The Problem is when i click the FindPrevious Button it is searching only the first word.

high_flyer
21st February 2011, 11:53
Your first statement is always taking the cursor to the end of the document, so every time it starts form the end again, which is why you only get the first word from the end.
You need to update the cursor position after every search, so that it will search from that position back, and not from the end of the document.

charlesprime
21st February 2011, 12:31
Oh, Yeh. I have changed that. But , the cursor is moving towards Left i.e FindPrevious until the start of the Document.I can see that the cursor is moving Towards Left,but it is not selecting words using the Wordundercursor.

high_flyer
21st February 2011, 12:44
I don't understand the while loop.
If this is a slot code, which each click should find the next word, in your loop you are going all the way back to the start of the document, all as a response to one click.

charlesprime
21st February 2011, 13:46
no no. I am using the While loop to search from Start to End(FindNext) OR End to Start(FindPrevious) of the Document. And i am using the IF Condition to search the matching text which was Entered by the User. If the entered text matches the text in the document, it will Display the text and using break statement the while loop ends up!!
Thanx for the reply, the FindPrevious function is working well now!!

high_flyer
21st February 2011, 14:07
using break statement the while loop ends up!!
I see no 'break' in the code you posted.


but it is not selecting words using the Wordundercursor.
but you position your cursor to the left of the word, so there is no word under the cursor at the beginning of the word.
Try QTextCursor::Right after the move you do with WordLeft, see if this helps.

MarekR22
21st February 2011, 14:13
Why don't you use that: QTextDocument::find.

high_flyer
21st February 2011, 14:25
Why don't you use that: QTextDocument::find.
Very true! :)

charlesprime
21st February 2011, 14:25
Thanks,High_flyer .i tried same way and now is working.
please help me,how do i highlight the selected words...
thanks!!!!

high_flyer
21st February 2011, 14:37
have a look here:
http://doc.trolltech.com/4.7/richtext-syntaxhighlighter.html

charlesprime
23rd February 2011, 06:32
Thank u!! FindNext and FindPrevious are working now. I used QTextDocument::find() . Am having a small problem now, while searching the text it will be hightlighted in some color. When i close the Dialog(find dialog) after searching the text , the color still remains in the textedit. Lets say we are using the Yellow color to highlight the text for finding the text, when i close the dialog , the yellow color will stay !!
Is there any way to clear the color after searching the text?????