3

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!

jww
  • 97,681
  • 90
  • 411
  • 885
Jettoblack
  • 125
  • 3
  • 8
  • Also see [Moving the cursor inside of QTextEdit](https://stackoverflow.com/q/11683311/608639). – jww Dec 17 '19 at 18:14

1 Answers1

6

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

int pos = lineEdit->cursorPosition();
// change text
lineEdit->setCursorPosition(pos);
Amartel
  • 4,248
  • 2
  • 15
  • 21
  • Thanks. This is of course the correct answer, but the tricky part was figuring out WHEN to save and restore the cursor position in terms of the chain of signals and slots that are occurring. I don't know if I overlooked something simple but eventually I figured it out. – Jettoblack Apr 18 '13 at 17:23