How can I scroll to the specified line in QPlainTextEdit?

Assuming I have the line number in the ln variable.

int ln=25;

      

When I pass ln to QPlainTextEdit, the scrollbar will scroll to line 25 in QPlainTextEdit.

How do you implement this feature? Can anyone give me some advice? I would be very grateful

+3


source to share


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







All Articles