Retain current cursor position in QLineEdit
-
Hi all,
I cannot manage to retain my current edit cursor position in QLineEdit widget.
I have a listbox (single selection). When a user clicks on the listbox, the selected item text will appear on the QLineEdit widget.
The retained current position fails after I append a character to the middle of the text. The cursor always moves to the end of text. If I move the cursor to the middle of the text again and append the text, the following cursor position works normally. Here is my code:
void Add_Expense_Type_Window::on_Expense_Type_List_itemSelectionChanged() { ui->Expense_Type_Field->setText(ui->Expense_Type_List->currentItem()->text()); set_clicked_listbox(true) }; void Add_Expense_Type_Window::on_Expense_Type_Field_textChanged(QString text_value) { if (get_clicked_listbox() == true) { ui->Expense_Type_Field->setText(text_value); set_clicked_listbox(false); } }
How shall I retain my cursor position when I append a character to the middle of the QLineEdit widget?
-
-
@Axel-Spoerl said in Retain current cursor position in QLineEdit:
@John-Hon
You can read the cursor position before and set it after the text modification.
See here.Is there way to prevent cursor position change if I do not want to involve cursorPositionChanged signal overwrite the function call?
-
@John-Hon
No, but you can block signals while you reset the cursor. -