QT-Qtextedit set cursor position

Overview

There is a series of articles about setting the cursor in Qtextedit. It was found that it was impossible to see. They are only talking about the direction, and the description is not comprehensive. A detailed summary and analysis are given below.

Summarize

set cursor position

The first:
if you're not particularly personal with the location settings, i.e. you just want to move to "beginning, end of this line", etc. You can use the following methods:
This is to move to the far left of the line
Note that myTextEdit is our QTextedit.

QTextCursor tmpCursor = ui.translationInput->textCursor();
tmpCursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 4);
ui.translationInput->setTextCursor(tmpCursor);

Move to the end of the text:

myTextEdit ->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);

Second:
If you want to specify to "a certain position in this line", then you can use:

QTextCursor tmpCursor = myTextEdit->textCursor();  tmpCursor.setPosition(20);
myTextEdit->setTextCursor(tmpCursor);

If you want to get the position of the cursor such as: line number, and position within a line. see

http://blog.csdn.net/OOFFrankDura/article/details/79611285

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325764692&siteId=291194637