How can I scroll to the specified line in QPlainTextEdit?
1 answer
Use QPlaintextEdit::document
to get QTextDocument
.
Use QTextDocument::findBlockByLineNumber
to get a QTextBlock
specific line number. Remember that it starts at line 0, not line 1.
Then build QTextCursor
with this QTextBlock
and set it to QPlainTextEdit
.
int ln=25;
QTextCursor cursor(p_textEdit->document()->findBlockByLineNumber(ln-1)); // ln-1 because line number starts from 0
p_textEdit->setTextCursor(cursor);
+5
source to share