How to move the caret to the end of the text in QTextEdit? Code example:

void MainWindow::on_mark_textChanged() { if((ui->mark->toPlainText()).size() > 2) { QString str = ui->mark->toPlainText(); str.chop(1); ui->mark->setText(str); } } 

Here the input text is truncated to two characters, and the carriage returns to the beginning.

    1 answer 1

     QTextCursor cursor = myQTextEdit.textCursor(); cursor.movePosition(QTextCursor::End); myQTextEdit.setTextCursor(cursor); 

    If this does not work, swap lines 2 and 3.

    Original answer