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. How to get the character at the current cursor position in QLineEdit
Forum Updated to NodeBB v4.3 + New Features

How to get the character at the current cursor position in QLineEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 12.8k Views 1 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.
  • L Offline
    L Offline
    lepepper
    wrote on last edited by
    #1

    Any idea how to get the character at the current cursor position in QLineEdit
    I was trying to do use Qtextcursor,But in vain.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      @
      QChar currentChar = lineEdit->text().at(lineEdit->cursorPosition());
      @

      This returns the char right of the vertical line indicating the cursor.

      currentChar is NOT valid, if the cursor is at the end of the line edit (i.e. no char right of the vertical line)

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        My first example was not quite correct. To cite Qt Docs

        bq. API docs on "QString::at() ":http://doc.qt.nokia.com/stable/qstring.html#at
        The position must be a valid index position in the string (i.e., 0 <= position < size()).

        So better have a check:

        @
        int currentPos = lineEdit->cursorPosition();
        QString currentText = lineEdit->text();
        QChar currentChar;
        if(currentPos < currentText.size())
        currentChar = currentText.at(currentPos);
        @

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lepepper
          wrote on last edited by
          #4

          Thanks,
          I was doing a delete operation,so on deletion of characters,the character to the left of cursor position (vertical line )was needed,any ideas

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            Come on - thats a "-1" to the current position and you have to check for "pos - 1 >= 0".

            That's a no-brainer - do you really need another sample code for this?

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Panke
              wrote on last edited by
              #6

              Or just select the part you want to delete and call selectedText()

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kishore_hemmady
                wrote on last edited by kishore_hemmady
                #7

                hi,
                Here is the same question ,
                how can i add a text at the current cursor position in QLineEdit?

                JonBJ 1 Reply Last reply
                0
                • K kishore_hemmady

                  hi,
                  Here is the same question ,
                  how can i add a text at the current cursor position in QLineEdit?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @kishore_hemmady
                  What does http://doc.qt.io/qt-5/qlineedit.html#insert do?

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kishore_hemmady
                    wrote on last edited by
                    #9

                    hi @JonB
                    I need to find the position of the cursor and append my text at that position.
                    your answer adds only new text,not at the defined position of a current text.

                    JonBJ 1 Reply Last reply
                    0
                    • K kishore_hemmady

                      hi @JonB
                      I need to find the position of the cursor and append my text at that position.
                      your answer adds only new text,not at the defined position of a current text.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @kishore_hemmady
                      It says:

                      Deletes any selected text, inserts newText,

                      so doesn't it insert at the current selection position?

                      1 Reply Last reply
                      2
                      • K Offline
                        K Offline
                        kishore_hemmady
                        wrote on last edited by
                        #11

                        thank you @JonB

                        1 Reply Last reply
                        0

                        • Login

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