4

I can get the current position in row and col via QTextCursor::blockNumber() and QTextCursor::positionInBlock(). My question is that how to move the cursor to the specific position with row and col. like

setPosition(x,y) // The current cursor would move to row x and col y.

Is it possible to do that?

3 Answers 3

8

Easy solution:

Just move the cursor there:

textEdit.moveCursor(QTextCursor::Start); //set to start
for( <... y-times ...> )
{
    textEdit.moveCursor(QTextCursor::Down); //move down
}
for( < ... x-times ...>) 
{
    textEdit.moveCursor(QTextCursor::Right); //move right
}

moveCursor is also the goto-way if you need to "select" text for changing it. A similar approach without the loops is also at the end.

A bit more explanation and maybe better solution:

In theory a text has no "lines" like shown in GUIs but the endline-character (\n or \r\n depending on operating system and framework) is only just another character. So for a cursor mostly everything is just one "text" without lines.

There are wrapper functions to deal with this but I get to them later. First you cannot access those directly via the QTextEdit interface but you have to manipulate the cursor directly.

QTextCursor curs = textEdit.textCursor(); //copies current cursor
//... cursor operations
textEdit.setTextCursor(curs);

Now for the "operations":

If you know at what position you want to go in the string you have setPosition() here. This "position" is not in respect to vertical lines though, but the whole text.

This is what a multi-line string looks internally:

"Hello, World!\nAnotherLine"

This would show

Hello, World!
AnotherLine

setPosition() wants the position of the internal string.

To move to another line you would have to calculate the position by looking for the first \n in the text and add your x-offset. If you want the 3rd line look for the first 2 \n etc.

Luckily there is also the function setVerticalMovement which seems to wrap this and maybe is what you want to do. It moves the cursor vertically.

So you could do:

curs.setPosition(x); //beginning at first line
curs.setVerticalMovement(y); //move down to the line you want.

After that call setTextCursor like shown above with the cursor.

Note:

The order is important though. setPosition sets the position in the whole text. So setPosition(5) while maybe in the 3rd line will not set it to the 5th character in the line you are but of the whole text. So move the x-cordinate first then y.


You need to be aware of the lengths of the lines though.

some longer line
short
another longer line

If you now specify line 2 and column 7 it will be "out-of-bounds". I am not sure how verticalMovement here behaves. I assume the cursor will be at the end of the line.


When you use the QTextCursor class directly you can also use the move operations without the loops because they have an extra parameter to repeat the operations.

curs.movePosition(QTextCursor::Start);
curs.movePosition(QTextCursor::Down,<modeMode>,y); //go down y-times
curs.movePosition(QTextCursor::Right,<moveMode>,x); //go right x-times
1
  • @Tarod thx. considering the length of the question this may be a bit too much though :D
    – Hayt
    Commented Oct 17, 2016 at 9:12
8

I think the best way is via QTextCursor.

For example, if your QTextEdit is called textEdit:

QTextCursor textCursor = ui->textEdit->textCursor();

textCursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, x);
textCursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, y);

ui->textEdit->setTextCursor(textCursor);

Where x and y are the required position.

4
  • 1
    you should move it to the start though at first.
    – Hayt
    Commented Oct 17, 2016 at 8:56
  • Depends on the application. I don't know the initial state. But yes, it could be necessary.
    – Tarod
    Commented Oct 17, 2016 at 8:57
  • well for consecutive calls at least.
    – Hayt
    Commented Oct 17, 2016 at 8:57
  • 1
    This works great, because you don't have to make loops + add .movePosition(QTextCursor::Start) Commented Oct 11, 2019 at 14:51
0

If the document layout uses 1 block per line (e.g. it's a QPlainTextEdit or the document contains text with very basic formatting), then you can set the position directly:

void setCursorLineAndColumn (QTextCursor &cursor, int line, int col, 
                             QTextCursor::MoveMode mode) 
{
    QTextBlock b = cursor.document()->findBlockByLineNumber(line);
    cursor.setPosition(b.position() + col, mode);
    // you could make it a one-liner if you really want to, i guess.
}

The major advantage over the Down + Right methods is it is much simpler to select text between two locations:

QTextCursor cursor = textEdit->textCursor();
setCursorLineAndColumn(cursor, startLine, startCol, QTextCursor::MoveAnchor);
setCursorLineAndColumn(cursor, endLine, endCol, QTextCursor::KeepAnchor);
textEdit->setTextCursor(cursor);

There's also a performance advantage, if relevant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.