Results 1 to 11 of 11

Thread: Problem in FINDING Previous Word

  1. #1
    Join Date
    Feb 2011
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Problem in FINDING Previous Word

    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!!!!!!

    Qt Code:
    1. cursor->movePosition(QTextCursor::End);
    2.  
    3. while(!cursor->atStart())
    4. {
    5.  
    6. //cursor->setCharFormat(format);
    7. cursor->select(QTextCursor::WordUnderCursor);
    8. QString s=cursor->selectedText();
    9.  
    10. if(s.contains(findlinededit->text(),Qt::CaseInsensitive))
    11. {
    12. .................//this is used to change the color of the text when it is found!!
    13. }
    14. cursor->movePosition(QTextCursor::WordLeft,QTextCursor::KeepAnchor);
    15.  
    16. }//end of while
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem in FINDING Previous Word

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2011
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem in FINDING Previous Word

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem in FINDING Previous Word

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Feb 2011
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem in FINDING Previous Word

    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!!

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem in FINDING Previous Word

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem in FINDING Previous Word

    Why don't you use that: QTextDocument::find.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem in FINDING Previous Word

    Why don't you use that: QTextDocument::find.
    Very true!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Feb 2011
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem in FINDING Previous Word

    Thanks,High_flyer .i tried same way and now is working.
    please help me,how do i highlight the selected words...
    thanks!!!!

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem in FINDING Previous Word

    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Feb 2011
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem in FINDING Previous Word

    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?????

Similar Threads

  1. how to get the word when i highlight a word with mouse?
    By saleh.hi.62 in forum Qt Programming
    Replies: 0
    Last Post: 4th July 2010, 08:24
  2. Finding whole word in QString ?
    By rajesh in forum Qt Programming
    Replies: 6
    Last Post: 20th November 2009, 11:26
  3. Problem finding text in a QTextEdit
    By Vash5556 in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2009, 14:19
  4. Path to the previous folder
    By Zergi in forum Newbie
    Replies: 2
    Last Post: 15th July 2008, 18:24
  5. Replies: 7
    Last Post: 15th February 2006, 11:34

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.