Question

I have a QTableView with one column that uses a QLineEdit as its editor delegate, and other columns that need to update dynamically as the user types into the QLineEdit (e.g. one of the columns contains the length of the text typed in the QLineEdit and it should update as the user types, not waiting for them to hit Enter to commit).

I used this code: Qt: Signal while a QTableView item data is being edited instead of after edit is done? which mostly works. It connects the QLineEdit textChanged() signal to the editor's commitData() signal.

The problem with this code is that as the user types, the QLineEdit's insertion cursor always jumps to the end. If you are appending text to the end of the line that's fine. But if the user wants to insert or edit text in the middle of the line, every time they type one letter, the text insertion cursor jumps to the end of the QLineEdit. After each keystroke they have to reposition the cursor in order to finish the insertion/edit in the middle.

If I disable the mapper then the cursor doesn't jump, so it isn't something inherent to the editor delegate; it only happens when using the code from the question linked above.

I looked at the code for QLineEdit textChanged() and commitData() but I can't figure out what is causing the cursor to jump to the end of the QLineEdit. Any ideas? Thanks!

Was it helpful?

Solution

You could remember last text cursor position and then set it manually like this:

int pos = lineEdit->cursorPosition();
// change text
lineEdit->setCursorPosition(pos);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top