How to allow QLineEdit detects Tab Key Pressing ev

2019-08-28 08:05发布

问题:

This post is to rephrase my question posted here in a different way. I want to make QlineEdit detect Tab Key press to call a method called do_something(). I generated the following pyqt5 code from Qt Designer which includes QlineEdit instance called lineEdit. When I enter SSN number and press tab key, the method should be called. How can I do that?

from PyQt5.QtWidgets import QApplication
from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(348, 68)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(40, 20, 41, 16))
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(80, 20, 201, 21))
        self.lineEdit.setObjectName("lineEdit")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def do_something():
        print('Success!')

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "SSN"))

回答1:

The simplest option as indicated in my previous answer is to use QShorcut, you must pass as a widget to the QLineEdit and a context that in this case must be Qt::WidgetWithChildrenShortcut

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(348, 68)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(40, 20, 41, 16))
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(80, 20, 201, 21))
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "SSN"))

class Dialog(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setupUi(self)
        shortcut = QtWidgets.QShortcut(
            QtGui.QKeySequence(QtCore.Qt.Key_Tab), 
            self.lineEdit, 
            context= QtCore.Qt.WidgetWithChildrenShortcut,
            activated=self.do_something)

    @QtCore.pyqtSlot()
    def do_something(self):
        print('Success!')

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Dialog()
    w.show()
    sys.exit(app.exec_())


回答2:

Much more simply, I had success with subclassing QLineEdit and over-riding its event method, as follows:

class EditSpec(QLineEdit):
    def event(self,event):
        if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Tab:
            self.tabFollow()
            return True
        else:
            return QLineEdit.event(self,event)          
    def tabFollow(self):
        print("tab-key pressed!")

Any non-tab key press proceeds normally, while tab-key presses result in the tabFollow method firing. To add (and this is important), over-riding the keyPressEvent method does not prevent the event from being fired through the event-system of the application. Only overriding the event method in a manner similar to the above did that.