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
Forum Updated to NodeBB v4.3 + New Features

Retain current cursor position in QLineEdit

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 525 Views 2 Watching
  • 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 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?

    Axel SpoerlA 1 Reply Last reply
    0
    • J John Hon

      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 SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on 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
      4
      • Axel SpoerlA Axel Spoerl

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

        Axel SpoerlA 1 Reply Last reply
        0
        • J John Hon

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

          Axel SpoerlA Offline
          Axel SpoerlA Offline
          Axel Spoerl
          Moderators
          wrote on 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

          • Login

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