如何允许QLineEdit检测Tab键按下事件

时间:2018-12-19 17:23:50

标签: python pyqt pyqt5

这篇文章是用另一种方式重新表达我在here上发布的问题。我想让QlineEdit检测到Tab Key被按下以调用称为do_something()的方法。我从Qt Designer生成了以下pyqt5代码,其中包括名为lineEdit的QlineEdit实例。当我输入SSN数字并按Tab键时,应调用该方法。我该怎么办?

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"))

2 个答案:

答案 0 :(得分:1)

我上一个答案中指出的最简单的选择是使用QShorcut,您必须将其作为小部件传递给QLineEdit,并且在这种情况下必须将上下文传递为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_())

答案 1 :(得分:0)

更简单地说,我成功地继承了QLineEdit的子类并覆盖了其事件方法,如下所示:

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!")

任何非Tab键的按下都会正常进行,而Tab键的按下会导致tabFollow方法触发。要添加(这很重要),重写keyPressEvent方法不会阻止事件通过应用程序的事件系统触发。只能以与上述类似的方式覆盖事件方法。