Qt: QLineEdit cursor moves to end after textChanged() or commitData()

好久不见. 提交于 2019-12-12 18:50:43

问题


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!


回答1:


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

int pos = lineEdit->cursorPosition();
// change text
lineEdit->setCursorPosition(pos);


来源:https://stackoverflow.com/questions/15801259/qt-qlineedit-cursor-moves-to-end-after-textchanged-or-commitdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!