Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. unable to change cursor position after document change
Qt 6.11 is out! See what's new in the release blog

unable to change cursor position after document change

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 546 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    django.Reinhard
    wrote on last edited by
    #1

    Hi,

    I'm working on an editor like codeeditor-example, which means QPlainTextEdit ...

    I use similar code to highlight current line ...

    On change, I check the file for unsupported characters, which I replace to supported characters. Then I save the file to disk and reload the (possibly patched) content back into editor.

    That all works so far.

    My issue is - that I can't restore cursor position after replacing content in editor.

    When I use editor->document()->setPainText( ... ) then cursor is positioned after then end of file and I can't move it from there.
    When I use editor->setPlainText( ... ) then the cursor is positioned before the first line, and I can't move it from there neither.

    I use editor->textCursor().setPosition( ... ). I even tried to replace QTextCursor with a new one, but that did not work.

    Changing cursor with mouseclick on a line works, but I can't set cursor programmatically.

    What am I missing?

    Gojir4G 1 Reply Last reply
    0
    • D django.Reinhard

      Hi,

      I'm working on an editor like codeeditor-example, which means QPlainTextEdit ...

      I use similar code to highlight current line ...

      On change, I check the file for unsupported characters, which I replace to supported characters. Then I save the file to disk and reload the (possibly patched) content back into editor.

      That all works so far.

      My issue is - that I can't restore cursor position after replacing content in editor.

      When I use editor->document()->setPainText( ... ) then cursor is positioned after then end of file and I can't move it from there.
      When I use editor->setPlainText( ... ) then the cursor is positioned before the first line, and I can't move it from there neither.

      I use editor->textCursor().setPosition( ... ). I even tried to replace QTextCursor with a new one, but that did not work.

      Changing cursor with mouseclick on a line works, but I can't set cursor programmatically.

      What am I missing?

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      hi @django-Reinhard ,

      You should "apply" the modification done to the cursor. When you are using editor->textCursor(), you receive a QTextCursor object by copy and not by reference, which means that the change are only applied locally but not applied to the QPlaintextEdit instance itself.

      This should fix your issue:

      QTextCursor tc = editor->textCursor();
      tc.setPosition(...);
      editor.setTextCursor(tc);
      
      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved