Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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


  • Moderators

    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().



  • @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).


  • Moderators

    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.


  • Banned

    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



  • @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 which QLineEdit has focus, but not cursor position there, which may or may not simplify.


  • Banned

    Okay well here is a simple version of what you stated you were trying to achieve using a dictionary to handle your Editable Boxes and while this could probably be made to be slicker by perhaps making the handling of the buttons a bit more generic it shows you the basic concept that you can easily build upon.

    try:
        from PyQt5.QtCore    import pyqtSlot
        from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout
        from PyQt5.QtWidgets import QLineEdit, QPushButton, QLabel
    
    except ImportError:
        from PySide2.QtCore    import Slot as pyqtSlot
        from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout
        from PySide2.QtWidgets import QLineEdit, QPushButton, QLabel
    
    # This is only needed for PySide2 and I rename
    # this for numerous reason but one does not have to
    from sys import exit as sysExit
    
    class MyLine(QLineEdit):
        def __init__(self, parent, id):
            QLineEdit.__init__(self)
            self.Parent = parent
            self.Id = id
    
        def focusInEvent(self, Evnt):
            self.Parent.LineId = self.Id
          # Should probably consider changing the border
          # color or background color of the currently
          # selected box just for clarity purposes
            print('I got focus', self.Id)
    
    class MainWindow(QWidget):
        def __init__(self):
            QWidget.__init__(self)
            Top=300; Left=700; Width=300; Hight=100
            self.setGeometry(Left, Top, Width, Hight)
            self.setWindowTitle('Line Fill')
    
            self.LineId = 1
    
            self.btnOne = QPushButton('1')
            self.btnOne.clicked.connect(self.ValueOne)
    
            self.btnTwo = QPushButton('2')
            self.btnTwo.clicked.connect(self.ValueTwo)
    
            HBox = QHBoxLayout()
            HBox.addWidget(self.btnOne)
            HBox.addWidget(QLabel('    ')) # Spacer
            HBox.addWidget(self.btnTwo)
            HBox.addStretch(1)
    
            self.Areas = {}
            self.Areas[1] = MyLine(self, 1)
            self.Areas[2] = MyLine(self, 2)
    
            VBox = QVBoxLayout()
            VBox.addWidget(self.Areas[1])
            VBox.addWidget(self.Areas[2])
            VBox.addWidget(QLabel(' ')) # Spacer
            VBox.addLayout(HBox)
           
            self.setLayout(VBox)
    
        def ValueOne(self):
            self.Areas[self.LineId].setText('1')
    
        def ValueTwo(self):
            self.Areas[self.LineId].setText('2')
            
    if __name__=="__main__":
        MainEventHandler = QApplication([])
    
        MainApp = MainWindow()
        MainApp.show()
    
        sysExit(MainEventHandler.exec_())
    
      # If anyone wants more extensive free help I run an online lab-like classroom-like 
      # message server feel free and drop by you will not be able to post until I clear 
      # you as a student as this prevents spammers so if interested here is the invite
      # https://discord.gg/3D8huKC
    


  • @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!



  • 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


Log in to reply