Search code examples
pythonqtpyqtbarcode-scanner

pyqt barcode scanner lineEdit


I'm using a USB-barcode scanner to set the text of a Qt lineEdit field, the text of which is then used for other features of the GUI (specifically, the text is the name of the sample currently being measured by the user and will be saved as a filename later).

My issue is that I want to dynamically overwrite the current text in the lineEdit field with the next scanned barcode, without the user having to delete the current text by hand before scanning. Because I am simply using the scanner as a keyboard emulator rather reading properly the serial info from it, the user has to click the text field before scanning.

I can't figure out which lineEdit connect action that would allow the following:

from PyQt4 import QtGui


# add widgets etc
# ...........

# lineEdit part
self.mylineEdit = QtGui.QLineEdit()

#initialise to empty string on start up
self.mylineEdit.setText(' ')


#barcode scans here and then a returnPressed is registered

#connect to a function
self.mylineEdit.returnPressed.connect(self.set_sample_name) #here is where I want to delete the previous entry without backspacing by hand


#set the sample name variable
def set_sample_name(self):
    self.sample_name = self.mylindEdit.text()

I'm wondering is there a way of deleting the previous string in the text box before the next barcode is scanned? (without the text field going empty for a time)..

Thanks.

PS - Using python3.5.2 and pyQT4 on Ubuntu 16.04


Solution

  • from PyQt5 import QtWidgets,QtCore
    import sys
    import os
    class window(QtWidgets.QMainWindow):
        def __init__(self):
            super(window,self).__init__()
            self.mylineEdit = QtWidgets.QLineEdit()
            self.mylineEdit2 = QtWidgets.QLineEdit()
            self.startNew=1
            #initialise to empty string on start up
            self.mylineEdit.setText(' ')
    
    
            #barcode scans here and then a returnPressed is registered
    
            #connect to a function
            self.mylineEdit.returnPressed.connect(self.set_sample_name) #here is where I want to delete the previous entry without backspacing by hand
            self.mylineEdit.textChanged.connect(self.delete_previous)
    
            centwid=QtWidgets.QWidget()
            lay=QtWidgets.QVBoxLayout()
            lay.addWidget(self.mylineEdit)
            lay.addWidget(self.mylineEdit2)
            centwid.setLayout(lay)
            self.setCentralWidget(centwid)
            self.show()
    
            #set the sample name variable
        def set_sample_name(self):
            self.sample_name = self.mylineEdit.text()
            print(self.sample_name)
            self.startNew=1
        def delete_previous(self,text):
            if self.startNew:
                self.mylineEdit.setText(text[-1])
                self.startNew=0
    app=QtWidgets.QApplication(sys.argv)
    ex=window()
    sys.exit(app.exec_())
    

    As soon as return pressed signal is executed you can change the flag self.startNew=1 which will ensure whenever the text will change it will check the flag and remove the complete string as soon as new barcode is entered. I have done in PyQt5 but the concept will remain the same. The functionality is achieved in self.myLineEdit.