Solved Find position of typing cursor?
-
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.
-
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
-
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().
-
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).
-
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.
-
@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. :(
-
@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 whichQLineEdit
has focus, but not cursor position there, which may or may not simplify. -
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!
-
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