Results 1 to 4 of 4

Thread: QTextCursor / replace selected text

  1. #1
    Join Date
    May 2011
    Posts
    132
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    14

    Default QTextCursor / replace selected text

    Can some one explain what I am doing wrong here ?. What I need is to select a text in QTextEdit and make some changes in the selected block texts.

    debug prints selected block texts just fine but only when I remove "cursor.insertText("replace block text");". Once added back it screwing up somehow the block numbers and the entire concept. How to do it right ?


    Qt Code:
    1. QTextCursor cursor = this->textCursor();
    2. int start = cursor.selectionStart();
    3. int end = cursor.selectionEnd();
    4.  
    5. if(!cursor.hasSelection())
    6. return;
    7.  
    8. cursor.setPosition(start, QTextCursor::KeepAnchor);
    9. int startLine = cursor.blockNumber();
    10.  
    11. cursor.setPosition(end, QTextCursor::KeepAnchor);
    12. int lastLine = cursor.blockNumber();
    13.  
    14. qDebug() << start << end;
    15.  
    16. for(int blockNUmber = startLine; blockNUmber <= lastLine; blockNUmber++)
    17. {
    18. QTextBlock block = document()->findBlockByNumber(blockNUmber);
    19. QString blockText = block.text();
    20.  
    21. qDebug() << blockNUmber << block.position() << blockText;
    22.  
    23. cursor.clearSelection();
    24. cursor.setPosition(block.position(),QTextCursor::KeepAnchor);
    25. cursor.insertText("replace block text");
    26. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    20
    Thanked 28 Times in 27 Posts

    Default Re: QTextCursor / replace selected text

    First you are iterating over blocks in textdocument. So wouldn't this piece of documentation on QTextBlock should be taken care of?
    The next() and previous() functions enable iteration over consecutive valid blocks in a document under the condition that the document is not modified by other means during the iteration process. Note that, although blocks are returned in sequence, adjacent blocks may come from different places in the document structure. The validity of a block can be determined by calling isValid().

  3. #3
    Join Date
    May 2011
    Posts
    132
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    14

    Default Re: QTextCursor / replace selected text

    Thanks. I made some changes. In the example below the text is modified only in the first block. And again removing line cursor.intertText() fixes iteration.

    What is wrong yet ? Appreciate for help !

    Qt Code:
    1. QTextCursor cursor = this->textCursor();
    2.  
    3. int start = cursor.selectionStart();
    4. int end = cursor.selectionEnd();
    5.  
    6. if(!cursor.hasSelection())
    7. return;
    8.  
    9. cursor.setPosition(end, QTextCursor::KeepAnchor);
    10. QTextBlock endBlock = cursor.block();
    11.  
    12. cursor.setPosition(start, QTextCursor::KeepAnchor);
    13. QTextBlock block = cursor.block();
    14.  
    15. for(; block.isValid() && !(endBlock < block); block = block.next())
    16. {
    17. if (!block.isValid())
    18. continue;
    19.  
    20. qDebug() << block.blockNumber() << block.position() << block.text();
    21.  
    22. cursor.clearSelection();
    23. cursor.setPosition(block.position() ,QTextCursor::KeepAnchor);
    24. cursor.insertText("replace block text");
    25. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2011
    Posts
    132
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    14

    Default Re: QTextCursor / replace selected text

    I found the problem. QTextCursor::setPosition() is not actually block position, so that was the issue. Best to operate using QTextCursor::movePosition()

    Qt Code:
    1. QTextCursor cursor = textCursor();
    2.  
    3. int start = cursor.selectionStart();
    4. int end = cursor.selectionEnd();
    5.  
    6. if(!cursor.hasSelection())
    7. return;
    8.  
    9. cursor.setPosition(end, QTextCursor::KeepAnchor);
    10. QTextBlock endBlock = cursor.block();
    11.  
    12. cursor.setPosition(start, QTextCursor::KeepAnchor);
    13. QTextBlock block = cursor.block();
    14.  
    15. for(; block.isValid() && !(endBlock < block); block = block.next())
    16. {
    17. if (!block.isValid())
    18. continue;
    19.  
    20. cursor.movePosition(QTextCursor::StartOfLine);
    21. cursor.clearSelection();
    22. cursor.insertText(" text ");
    23. cursor.movePosition(QTextCursor::NextBlock);
    24. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by migel; 24th December 2012 at 16:17.

Similar Threads

  1. Replies: 3
    Last Post: 5th January 2012, 19:43
  2. Replies: 3
    Last Post: 23rd May 2011, 15:36
  3. Replies: 3
    Last Post: 3rd May 2009, 09:58
  4. QString - find and replace text
    By graciano in forum Newbie
    Replies: 3
    Last Post: 24th January 2009, 21:35
  5. Replace text
    By Hz in forum Qt Programming
    Replies: 2
    Last Post: 22nd March 2006, 09:16

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.