本文介绍了如何让 QLineEdit 检测 Tab 键按下事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章是为了改写我发布的问题 这里以不同的方式.我想让 QlineEdit 检测 Tab Key 按下以调用一个名为 do_something() 的方法.我从 Qt Designer 生成了以下 pyqt5 代码,其中包括名为 lineEdit 的 QlineEdit 实例.当我输入 SSN 号码并按 Tab 键时,应该调用该方法.我该怎么做?

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

推荐答案

如我之前的回答所示,最简单的选择是使用 QShorcut,您必须作为小部件传递给 QLineEdit 和上下文,在这种情况下必须是 Qt::WidgetWithChildrenShortcut

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_())

这篇关于如何让 QLineEdit 检测 Tab 键按下事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 11:28