PDA

View Full Version : Making arrow keys change cursor position in QTextEdit object



stevek
29th October 2016, 17:52
Qt 5.5.1

My objective is to display the line and column of the cursor in a QTextEdit object. It works properly using keys like enter and backspace to navigate, but the arrow keys seem to move the caret without changing the cursor position (the display for line and column number will stay the same despite the caret moving elsewhere for this reason). How can I make it so the arrow keys change the cursor position so I can update my label?

Here is my code that updates the display when the cursor position changes:


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

// ...

void MainWindow::on_textEdit_cursorPositionChanged()
{
ui->statusBar->showMessage("Line " + QString::number(cursor.blockNumber() + 1)
+ ", Col " + QString::number(cursor.positionInBlock() + 1));
}

anda_skoa
29th October 2016, 18:26
Interesting, as far as I can tell the cursor position should be updated.

Have you tried connecting to the QTextDocument::cursorPositionChanged() signal instead?
You get the document through QTextEdit::document().

Cheers,
_

P.S.: General recommendation: avoid using "connect by name", it can easily when you rename objects in designer.
Always prefer explicit connects.

stevek
30th October 2016, 04:53
I got exactly the same behavior after connecting that way.