Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Find position of typing cursor?
Forum Updated to NodeBB v4.3 + New Features

Find position of typing cursor?

Scheduled Pinned Locked Moved Solved Qt for Python
8 Posts 5 Posters 2.5k 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
    lolcocks
    wrote on last edited by
    #1

    Hello,

    I have a simple program running on PySide2, which has 5 lineEdit widgets.
    On the bottom I have given a numpad with numbers 0 to 9 as pushButtons.

    Whenever any of the numbers is pressed, I want that number to be entered into the lineEdit widget.

    Any way I can get the current location of where my typing cursor is placed?

    Regards.

    1 Reply Last reply
    0
    • eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #8

      I have implemented in a generic way using events avoiding having to know which QLineEdit you want to set the text for, and you can also use other widgets without modifications on the Keypad. The code is in my SO answer: https://stackoverflow.com/a/54259654/6622587

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      2
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #2

        Current line edit will return true if you call focus() on it (or check focus property). See the docs.

        You can also check cursor position inside the line edit using cursorPosition().

        (Z(:^

        L 1 Reply Last reply
        4
        • sierdzioS sierdzio

          Current line edit will return true if you call focus() on it (or check focus property). See the docs.

          You can also check cursor position inside the line edit using cursorPosition().

          L Offline
          L Offline
          lolcocks
          wrote on last edited by
          #3

          @sierdzio

          Once again, you have come to my rescue.
          The focus() method is really helpful.

          But is there any method that will give me the lineEdit which currently has the focus?

          Or better, is there a method that is called every time a lineEdit is focused?

          Once again I apologize if these questions are newbie. The documentation is well detailed but quite complex to search in (at least in my opinion, must be my newbie level).

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #4

            You can loop through parent widget's children() list to find the widget which has keyboard focus.

            I don't think there is a signal which is fired when focus changes, probably for performance reasons, I don't know.

            No worries, that's what this forum is for :-) Ask as much as you need.

            (Z(:^

            L 1 Reply Last reply
            1
            • sierdzioS sierdzio

              You can loop through parent widget's children() list to find the widget which has keyboard focus.

              I don't think there is a signal which is fired when focus changes, probably for performance reasons, I don't know.

              No worries, that's what this forum is for :-) Ask as much as you need.

              L Offline
              L Offline
              lolcocks
              wrote on last edited by lolcocks
              #5

              @sierdzio said in Find position of typing cursor?:

              You can loop through parent widget's children() list to find the widget which has keyboard focus.

              I don't think there is a signal which is fired when focus changes, probably for performance reasons, I don't know.

              No worries, that's what this forum is for :-) Ask as much as you need.

              That could work. Thank you. :)

              @Denni-0 said in Find position of typing cursor?:

              Not sure why you are doing this but a simpler method would be to assign each one of you Line Edits with an Id -- or maybe put all of these Line Edits into a Dictionary -- or perhaps something else but without knowing exactly what you are doing nor why it is hard to advise you on the best coding practice to employ for this. So can you provide more details about what you are attempting to achieve and why

              In the first post I said what I was doing. Maybe I wasn't clear enough.

              I have 5 lineEdit widgets on my application.

              And on the bottom there are pushButtons from 0 to 9.

              The application is going to be on a touch screen device.
              The user will tap on the first lineEdit, then he will press the pushButton "5". When he pushes the button, 5 will get typed into the first lineEdit as that's the one which has focus.

              I would have gladly posted a screenshot, but all my work is in the office and office is closed due to COVID-19. :(

              JonBJ 1 Reply Last reply
              0
              • L lolcocks

                @sierdzio said in Find position of typing cursor?:

                You can loop through parent widget's children() list to find the widget which has keyboard focus.

                I don't think there is a signal which is fired when focus changes, probably for performance reasons, I don't know.

                No worries, that's what this forum is for :-) Ask as much as you need.

                That could work. Thank you. :)

                @Denni-0 said in Find position of typing cursor?:

                Not sure why you are doing this but a simpler method would be to assign each one of you Line Edits with an Id -- or maybe put all of these Line Edits into a Dictionary -- or perhaps something else but without knowing exactly what you are doing nor why it is hard to advise you on the best coding practice to employ for this. So can you provide more details about what you are attempting to achieve and why

                In the first post I said what I was doing. Maybe I wasn't clear enough.

                I have 5 lineEdit widgets on my application.

                And on the bottom there are pushButtons from 0 to 9.

                The application is going to be on a touch screen device.
                The user will tap on the first lineEdit, then he will press the pushButton "5". When he pushes the button, 5 will get typed into the first lineEdit as that's the one which has focus.

                I would have gladly posted a screenshot, but all my work is in the office and office is closed due to COVID-19. :(

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

                @lolcocks said in Find position of typing cursor?:

                I have no idea whether this works/helps, but as an alternative to trying to "Find position of typing cursor" I wondered whether you might leverage copy/paste? https://doc.qt.io/qt-5/qtextedit.html#paste

                Pastes the text from the clipboard into the text edit at the current cursor position.

                So you could use QClipboard to set the text, then this. I guess you still need to know which QLineEdit has focus, but not cursor position there, which may or may not simplify.

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lolcocks
                  wrote on last edited by
                  #7

                  @Denni-0,

                  Oh man. This is perfect. Not only did you solve my issue, you also provided my with sample code to understand it better.

                  I cannot thank you enough.

                  To be precise, "focusInEvent" was exactly what I was looking for. Thank you so much!

                  1 Reply Last reply
                  0
                  • eyllanescE Offline
                    eyllanescE Offline
                    eyllanesc
                    wrote on last edited by
                    #8

                    I have implemented in a generic way using events avoiding having to know which QLineEdit you want to set the text for, and you can also use other widgets without modifications on the Keypad. The code is in my SO answer: https://stackoverflow.com/a/54259654/6622587

                    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                    1 Reply Last reply
                    2

                    • Login

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