Results 1 to 17 of 17

Thread: QTextCursor and beginEditBlock

  1. #1
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default QTextCursor and beginEditBlock

    Hello,

    I have some text in QPlainTextEdit, where every line starts with 10 spaces:

    Qt Code:
    1. // example:
    2.  
    3. line1
    4. line2
    5. line3
    6. line4
    To copy to clipboard, switch view to plain text mode 

    Then, I select few lines and in a loop I want to remove first two spaces from all the selected lines:

    Qt Code:
    1. // finding the startBlock and endBlock (+1)
    2.  
    3. cursor.beginEditBlock();
    4. for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
    5. cursor.setPosition(block.position());
    6. cursor.setPosition(block.position() + 2, QTextCursor::KeepAnchor);
    7. cursor.removeSelectedText();
    8. }
    9. cursor.endEditBlock();
    To copy to clipboard, switch view to plain text mode 

    The problem is that the code above “damages” the last selected line – as if it removed some kind of end-of-line marker – when I want to jump to the end of last line the cursor moves to the line below it, between first and second character. Even the selection does not show up properly after the edit – all the lines but the last one have selection indicator expanded to the right window edge and the last line has the indicator only as wide as the line.

    Qt Code:
    1. // after removing the first two characters:
    2.  
    3. line1 < 1. selected lines, run the code
    4. line2 <
    5. line3 < < 2. here I jump to end of line
    6. | line4
    7.  
    8. ^ 3. cursor appears here
    To copy to clipboard, switch view to plain text mode 

    When I remove `beginEditBlock()` and `endEditBlock()` everything works fine.

    Please, does anyone know why is this happening?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextCursor and beginEditBlock

    Please prepare a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default Re: QTextCursor and beginEditBlock


  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextCursor and beginEditBlock

    This works correctly for me:

    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. int main(int argc, char **argv) {
    4. QApplication app(argc, argv);
    5. QPlainTextEdit edit;
    6. edit.insertPlainText(" aaa\n aaa\n aaa\n aaa");
    7. QTextBlock block = edit.document()->findBlock(0);
    8. QTextCursor cursor = edit.textCursor();
    9. cursor.beginEditBlock();
    10. for (int i = 0; i < 3; i++) {
    11. cursor.setPosition(block.position());
    12. cursor.setPosition(block.position() + 2, QTextCursor::KeepAnchor);
    13. cursor.removeSelectedText();
    14. block = block.next();
    15. }
    16. cursor.endEditBlock();
    17. edit.show();
    18. return app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default Re: QTextCursor and beginEditBlock

    Thank you for the reply!

    Yes, it works - but I don't understand...how is it different from the example I wrote? Does the example work for you?

    The problem is, I would like the removing to be trigged by shortcut, not sure how to translate your code because I'll basically end up with what I already have, no? Sorry, I'm really confused why your code works and mine does not.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextCursor and beginEditBlock

    The only difference I can see between your code and mine is that in my case the code is ran before the widget is first shown on screen (thus before the document is first laid out)
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default Re: QTextCursor and beginEditBlock

    Hmm....exactly. That's really unfortunate as I need to run the code after the widget is drawn. Please, do you have any idea why is this happening?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextCursor and beginEditBlock

    We have no idea yet what is happening. You didn't verify that my code breaks when you first show the widget (and process its events) and then modify content.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default Re: QTextCursor and beginEditBlock

    You didn't verify that my code breaks when you first show the widget (and process its events) and then modify content.
    It breaks. (when I move "edit.show();" somewhere above "cursor.beginEditBlock();", that is...)

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextCursor and beginEditBlock

    show() only schedules the widget for display. It does not display it. You need to process events first. Either way, if it breaks then maybe something is wrong with the document layout engine.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default Re: QTextCursor and beginEditBlock

    I'm sorry I'm not sure I understand - I'm very new to Qt. Do you mean something like:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QtWidgets>
    3.  
    4. int main(int argc, char **argv) {
    5. QApplication app(argc, argv);
    6. QPlainTextEdit edit;
    7. edit.insertPlainText(" aaa\n aaa\n aaa\n aaa");
    8. edit.show();
    9.  
    10. app.processEvents();
    11.  
    12. QTextBlock block = edit.document()->findBlock(0);
    13. QTextCursor cursor = edit.textCursor();
    14. cursor.beginEditBlock();
    15. for (int i = 0; i < 3; i++) {
    16. cursor.setPosition(block.position());
    17. cursor.setPosition(block.position() + 2, QTextCursor::KeepAnchor);
    18. cursor.removeSelectedText();
    19. block = block.next();
    20. }
    21. cursor.endEditBlock();
    22.  
    23. return app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

    Also, the code I posted processes all the events and then modifies the code, is this correct?
    Last edited by ecir.hana; 6th March 2013 at 23:29.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextCursor and beginEditBlock

    I don't see how beginEditBlock() could break anything since all it does is to increment two internal counters. endEditBlock() correctly decrements the one that needs it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default Re: QTextCursor and beginEditBlock

    I don't see how beginEditBlock() could break anything
    The code I posted, does it work for you?

  14. #14
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTextCursor and beginEditBlock

    Try your code with a QTextEdit.

  15. #15
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default Re: QTextCursor and beginEditBlock

    In QTextEdit it works. But it would be really great if it worked in QPlainTextEdit too...

  16. #16
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTextCursor and beginEditBlock

    Quote Originally Posted by ecir.hana View Post
    But it would be really great if it worked in QPlainTextEdit too...
    Why so? I'm interested because another poster said the same thing (link) about a scrolling problem he was having with QPlainTextEdit.
    Last edited by norobro; 8th March 2013 at 19:06. Reason: typo

  17. #17
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default Re: QTextCursor and beginEditBlock

    Why so?
    Well...because it seems it's a bug and because QPlainTextEdit si faster than QTextEdit.

Similar Threads

  1. QTextCursor does not move
    By dotjan in forum Qt Programming
    Replies: 3
    Last Post: 16th November 2012, 10:00
  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. QTextCursor and QScriptEngine
    By roxton in forum Qt Programming
    Replies: 1
    Last Post: 31st October 2008, 19:47
  5. QTextCursor - setTextColor()
    By Dalamar in forum Newbie
    Replies: 0
    Last Post: 20th February 2006, 18:34

Tags for this Thread

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.