Transparently get backspace event in PyQt

前端 未结 1 934
滥情空心
滥情空心 2021-01-22 02:37

I want to detect when backspace is pressed in a QPlainTextEdit widget.

I have the following code:

    def keyPres         


        
相关标签:
1条回答
  • 2021-01-22 03:38

    The keyPressEvent method of QPlainTextEdit already has a certain behavior that among other things is to add text, but when you overwrite it you are eliminating it, so the solution is to call the implementation of the parent not to eliminate that behavior

    from PyQt5 import QtCore, QtWidgets
    
    class PlainTextEdit(QtWidgets.QPlainTextEdit):
        def keyPressEvent(self, event):
            if event.key() == QtCore.Qt.Key_Backspace:
                print("Backspace pressed")
            super(PlainTextEdit, self).keyPressEvent(event)
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        w = PlainTextEdit()
        w.show()
        sys.exit(app.exec_())
    
    0 讨论(0)
提交回复
热议问题