Nominate our 2022 Qt Champions!

QTextCursor::setPosition() not working in my QTextEdit.



  • Hi, I cannot manage to move textCursor of QTextEdit with setPosition function.
    The following instruction does not work in my Qt 5.10.0:

    this->textCursor().setPosition(0);
    

    Nothing happens. I want to move cursor to specific position in the text already written, thats why i'm not using movePosition function.

    Thanks.


  • Lifetime Qt Champion

    You need to set the cursor with setTextCursor(). textCursor only returns the current QTextCursor. See documentation: http://doc.qt.io/qt-5/qtextedit.html#textCursor

    "Returns a copy of the QTextCursor that represents the currently visible cursor. Note that changes on the returned cursor do not affect QTextEdit's cursor; use setTextCursor() to update the visible cursor."



  • Thanks Christian.
    So the code is like this:

     tbx->setFocus();
     QTextCursor cursor = tbx->textCursor();
     cursor.setPosition(5);
     tbx->setTextCursor(cursor);
    

Log in to reply