You can move to an absolute position within the document with
Code: Select all
QTextCursor::setPosition(int pos, QTextCursor::MoveMode m = MoveAnchor)
Setting it to an arbitrary line and column (assuming by
line y you mean
block y and by
column x you mean
xth character of block y) then you could always do this to position the cursor at (column x, line y) assuming both line and column are 1-based (i.e. start at 1 rather than 0)
Code: Select all
QTextCursor cursor = ui->plainTextEdit->textCursor();
cursor.movePosition(QTextCursor::Start);
if (y > 1)
cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, y-1);
if (x > 1)
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, x-1);
ui->plainTextEdit->setTextCursor(cursor); // move the edit's cursor to the new position
ui->plainTextEdit->insertPlainText("*");
I moved the widget's cursor at the end so that
plainTextEdit->insertPlainText() will insert at the selected position.
movePosition() will return
false if it fails to make the requested number of movements (like reaching end-of-document).