Results 1 to 4 of 4

Thread: QTextCursor does not move

  1. #1
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default QTextCursor does not move

    Hi, I am using QTextCursor to move my cursor at the Start and at the End of my QTextEdit document.

    Strangely, while I can move it to the Start with QTextCursor::Start, it seems it does not move to the end with QTextCursor::End

    Here is my code

    Qt Code:
    1. void MyPage::find(const QString &text, QTextDocument::FindFlags flags)
    2. {
    3. if ( !uiL->textEdit->find(text, flags) ) {
    4.  
    5. // if searching backward move cursor at the end
    6. if ( flags == QTextDocument::FindBackward ) {
    7.  
    8. QTextCursor mycursor;
    9. uiL->textEdit->setTextCursor(mycursor);
    10. mycursor.movePosition(QTextCursor::End); // THIS DOES NOT WORK, OR AT LEAST IT SEEMS SO
    11. }
    12.  
    13. // if searching foreward move cursor at the start
    14. if ( flags != QTextDocument::FindBackward ) {
    15. QTextCursor mycursor;
    16. uiL->textEdit->setTextCursor(mycursor);
    17. mycursor.movePosition(QTextCursor::Start); // THIS WORKS
    18. }
    19.  
    20. // once cursor is moved search again. If return false than the word does not exist in the doc
    21. if ( !uiL->textEdit->find(text, flags) ) {
    22. qDebug()<<"Not found";
    23. }
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    So basically when I am searching backward I always get "Not Found" when reached the top, I would expect to find the first occurrence from the bottom instead. DOes it make sense?

    Thanks,
    Jan

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextCursor does not move

    Quote Originally Posted by dotjan View Post
    Hi, I am using QTextCursor to move my cursor at the Start and at the End of my QTextEdit document.

    Strangely, while I can move it to the Start with QTextCursor::Start, it seems it does not move to the end with QTextCursor::End

    Here is my code

    Qt Code:
    1. void MyPage::find(const QString &text, QTextDocument::FindFlags flags)
    2. {
    3. if ( !uiL->textEdit->find(text, flags) ) {
    4.  
    5. // if searching backward move cursor at the end
    6. if ( flags == QTextDocument::FindBackward ) {
    7.  
    8. QTextCursor mycursor;
    9. mycursor.movePosition(QTextCursor::End); // try here instead
    10. uiL->textEdit->setTextCursor(mycursor);
    11. }
    12.  
    13. // if searching foreward move cursor at the start
    14. if ( flags != QTextDocument::FindBackward ) {
    15. QTextCursor mycursor;
    16. uiL->textEdit->setTextCursor(mycursor);
    17. mycursor.movePosition(QTextCursor::Start); // THIS WORKS - actually, this doesnt do anything...
    18. }
    19.  
    20. // once cursor is moved search again. If return false than the word does not exist in the doc
    21. if ( !uiL->textEdit->find(text, flags) ) {
    22. qDebug()<<"Not found";
    23. }
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    So basically when I am searching backward I always get "Not Found" when reached the top, I would expect to find the first occurrence from the bottom instead. DOes it make sense?

    Thanks,
    Jan
    see code changes

    do you know the reason why your way cannot work?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default Re: QTextCursor does not move

    Hi, actually it does not work either with your changes. But I have now found the solution by a chance..

    Qt Code:
    1. if ( !uiL->textEdit->find(text, flags) ) {
    2.  
    3. // if searching foreward, set backward flag and move cursor at the start
    4. if ( flags == QTextDocument::FindBackward ) {
    5. QTextCursor mycursor = uiL->textEdit->textCursor();
    6. mycursor.movePosition(QTextCursor::End);
    7. uiL->textEdit->setTextCursor(mycursor);
    8. }
    9.  
    10. if ( flags != QTextDocument::FindBackward ) {
    11. QTextCursor mycursor = uiL->textEdit->textCursor();
    12. mycursor.movePosition(QTextCursor::Start);
    13. uiL->textEdit->setTextCursor(mycursor);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextCursor does not move

    you move cursor to end when searching backwards - it doesn't match your comment.

    did you understand why your initial way will never work - it is an important concept/mechanism of c++!
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Problem in Move Move Event Handler.
    By redgoof in forum Qt Programming
    Replies: 0
    Last Post: 7th April 2010, 11:45
  2. QtextCursor
    By bismitapadhy in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2009, 12:09
  3. QTextCursor
    By bismitapadhy in forum Qt Programming
    Replies: 1
    Last Post: 18th June 2009, 11:46
  4. Move Rectangle on mouse Move
    By vermarajeev in forum Qt Programming
    Replies: 24
    Last Post: 14th May 2007, 05:34
  5. QTextCursor - setTextColor()
    By Dalamar in forum Newbie
    Replies: 0
    Last Post: 20th February 2006, 18: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
  •  
Qt is a trademark of The Qt Company.