Rhasta Shaman
Rhasta Shaman

Reputation: 15

Barcode Scanner tabbing to next line edit

I'm using a USB-barcode scanner to set the text of a Qt lineEdit field to scan production order number, My issue is that after scanning the window is closed instead of shifting to next lineEdit_2 to scan item number. How to setup lineEdit to shift the crusor to next lineEdit_2 and be ready for the next scan.

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

Upvotes: 1

Views: 270

Answers (1)

eyllanesc
eyllanesc

Reputation: 243955

By default the buttons of a QPushButton are pressed when the enter key is clicked and the QDialog has focus, and that click causes the rejected or accepted signal to be emitted, which closes the window, and this is handled by the default and autoDefault properties, so the first thing is to override that behavior.

On the other hand, pressing the enter key does not move to the other QLineEdit, in that case you must listen to that key and use focusNextPrevChild to move the focus.

Finally the codes generated by QtDesigner should not be modified so I will assume that the code you show is in the gui.py file, and I will implement the logic in main.py:

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

Upvotes: 2

Related Questions