[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?
-
@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.