Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Retain current cursor position in QLineEdit
QtWS25 Last Chance

Retain current cursor position in QLineEdit

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 453 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Hon
    wrote on 15 Jan 2024, 03:05 last edited by
    #1

    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?

    A 1 Reply Last reply 15 Jan 2024, 05:30
    0
    • J John Hon
      15 Jan 2024, 03:05

      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?

      A Offline
      A Offline
      Axel Spoerl
      Moderators
      wrote on 15 Jan 2024, 05:30 last edited by
      #2

      @John-Hon
      You can read the cursor position before and set it after the text modification.
      See here.

      Software Engineer
      The Qt Company, Oslo

      J 1 Reply Last reply 15 Jan 2024, 09:49
      4
      • A Axel Spoerl
        15 Jan 2024, 05:30

        @John-Hon
        You can read the cursor position before and set it after the text modification.
        See here.

        J Offline
        J Offline
        John Hon
        wrote on 15 Jan 2024, 09:49 last edited by
        #3

        @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?

        A 1 Reply Last reply 15 Jan 2024, 11:39
        0
        • J John Hon
          15 Jan 2024, 09:49

          @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?

          A Offline
          A Offline
          Axel Spoerl
          Moderators
          wrote on 15 Jan 2024, 11:39 last edited by
          #4

          @John-Hon
          No, but you can block signals while you reset the cursor.

          Software Engineer
          The Qt Company, Oslo

          1 Reply Last reply
          2
          • J John Hon has marked this topic as solved on 15 Jan 2024, 13:51

          3/4

          15 Jan 2024, 09:49

          • Login

          • Login or register to search.
          3 out of 4
          • First post
            3/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved