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

[PySide2] How do I send backspace as a QKeyEvent?



  • Hello,

    I have created a virtual keyboard in my application using PySide2.

    And I am using the QKeyEvent to pass the key that is being pressed using the following code:

            button = self.sender()
            widget = QApplication.focusWidget()
            
            if (button.text() == "\u232b"):
                widget.backspace()
            elif (button.text() == "Space"):
                text = " "
            elif (button.text() == "&&"):
                text = "&"
            else:
                text = button.text()
            
            
            if(button.text() != "\u232b"):
                key = button.property("_key_")
                
                if hasattr(key, "toPyObject"):
                    key = key.toPyObject()
                
                if widget:
                    event = QtGui.QKeyEvent(
                        QtCore.QEvent.KeyPress, key, QtCore.Qt.NoModifier, text
                    )
                    QtCore.QCoreApplication.postEvent(widget, event)
    

    My question is that how can I pass the backspace key?
    I have a backspace button which runs the function of .backspace() which works on LineEdits but is limited to that. Other widgets do not have this option, such as the dateEdit widget.

    Any suggestions?



  • @lolcocks mmm, Your code is unclear so it would be great if you provide a Minimum Reproducible Example (MRE), on the other hand you must send key = QtCore.Qt.Key_Backspace


  • Banned

    These might be javascript keycodes but they are most likely the standard keyboard codes for most keyboards so you should be able to use these codes for your virtual keyboard

    https://css-tricks.com/snippets/javascript/javascript-keycodes/



  • @lolcocks mmm, Your code is unclear so it would be great if you provide a Minimum Reproducible Example (MRE), on the other hand you must send key = QtCore.Qt.Key_Backspace


  • Banned

    @lolcocks sending QtCore.Qt.Key_Backspace simply equates to what you are doing using the actual key-codes so it is not a must use -- now grant you these key-codes have already been enumerated for you via this method but it all kind of depends on why you are doing what you are doing as to what the best solution would be for your program so use the option you feel is best.



  • @eyllanesc said in [PySide2] How do I send backspace as a QKeyEvent?:

    @lolcocks mmm, Your code is unclear so it would be great if you provide a Minimum Reproducible Example (MRE), on the other hand you must send key = QtCore.Qt.Key_Backspace

    @Denni-0 said in [PySide2] How do I send backspace as a QKeyEvent?:

    @lolcocks sending QtCore.Qt.Key_Backspace simply equates to what you are doing using the actual key-codes so it is not a must use -- now grant you these key-codes have already been enumerated for you via this method but it all kind of depends on why you are doing what you are doing as to what the best solution would be for your program so use the option you feel is best.

    Thank you both of you.
    I am going to go with eyllanesc's solution as it works the best for me. You know, because it sends an actual backspace key.


  • Banned

    @lolcocks said in [PySide2] How do I send backspace as a QKeyEvent?:

    I am going to go with eyllanesc's solution as it works the best for me. You know, because it sends an actual backspace key.

    No it does not actually send a backspace key -- it sends a backspace key-code -- it simply dresses that key-code up within a sort-of enumerated value -- which is what I was trying to explain -- so again basically no different than you sending the key-code via another method I could provide you with a MUC (Minimal Usable Code) example demonstrating that they are in fact the same thing if you need help understanding that there is no difference

    Lastly yes I concur using the Qt Enumerated value is much simpler and more straightforward and I am not saying do not use it. I am just concerned that you have the wrong idea about what it is doing and not doing.


Log in to reply