unable to change cursor position after document change
-
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 useeditor->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?
-
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);