条形码扫描仪制表符到下一行编辑
Barcode Scanner tabbing to next line edit
我正在使用 USB 条形码扫描仪设置 Qt lineEdit 字段的文本以扫描生产订单号,我的问题是扫描后 window 关闭而不是转移到下一个 lineEdit_2 扫描项目编号。如何设置 lineEdit 以将光标移动到下一个 lineEdit_2 并为下一次扫描做好准备。
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(290, 20, 81, 241))
self.buttonBox.setOrientation(QtCore.Qt.Vertical)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(50, 50, 113, 22))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_2.setGeometry(QtCore.QRect(50, 120, 113, 22))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_3.setGeometry(QtCore.QRect(50, 200, 113, 22))
self.lineEdit_3.setObjectName("lineEdit_3")
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
默认情况下,当单击回车键并且 QDialog 具有焦点时,会按下 QPushButton 的按钮,并且该单击会导致发出拒绝或接受的信号,从而关闭 window,这由 default 和 autoDefault 属性处理,所以第一件事就是覆盖该行为。
另一方面,按下回车键不会移动到另一个 QLineEdit,在这种情况下,您必须监听该键并使用 focusNextPrevChild 移动焦点。
最后QtDesigner生成的代码应该不会被修改所以我会假设你展示的代码在gui.py文件中,我会在main.py:[=11中实现逻辑=]
import sys
from PyQt5 import QtCore, QtWidgets
from gui import Ui_Dialog
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.show()
for btn in self.buttonBox.buttons():
btn.setDefault(False)
btn.setAutoDefault(False)
btn.setFocusPolicy(QtCore.Qt.NoFocus)
def keyPressEvent(self, event):
if event.key() in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return):
self.focusNextPrevChild(True)
super().keyPressEvent(event)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Dialog()
w.show()
sys.exit(app.exec_())
我正在使用 USB 条形码扫描仪设置 Qt lineEdit 字段的文本以扫描生产订单号,我的问题是扫描后 window 关闭而不是转移到下一个 lineEdit_2 扫描项目编号。如何设置 lineEdit 以将光标移动到下一个 lineEdit_2 并为下一次扫描做好准备。
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(290, 20, 81, 241))
self.buttonBox.setOrientation(QtCore.Qt.Vertical)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(50, 50, 113, 22))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_2.setGeometry(QtCore.QRect(50, 120, 113, 22))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_3.setGeometry(QtCore.QRect(50, 200, 113, 22))
self.lineEdit_3.setObjectName("lineEdit_3")
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
默认情况下,当单击回车键并且 QDialog 具有焦点时,会按下 QPushButton 的按钮,并且该单击会导致发出拒绝或接受的信号,从而关闭 window,这由 default 和 autoDefault 属性处理,所以第一件事就是覆盖该行为。
另一方面,按下回车键不会移动到另一个 QLineEdit,在这种情况下,您必须监听该键并使用 focusNextPrevChild 移动焦点。
最后QtDesigner生成的代码应该不会被修改所以我会假设你展示的代码在gui.py文件中,我会在main.py:[=11中实现逻辑=]
import sys
from PyQt5 import QtCore, QtWidgets
from gui import Ui_Dialog
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.show()
for btn in self.buttonBox.buttons():
btn.setDefault(False)
btn.setAutoDefault(False)
btn.setFocusPolicy(QtCore.Qt.NoFocus)
def keyPressEvent(self, event):
if event.key() in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return):
self.focusNextPrevChild(True)
super().keyPressEvent(event)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Dialog()
w.show()
sys.exit(app.exec_())