I'm fairly new to threading/GUIs and what I'm trying to create is a dialog that asks the user to scan a barcode (through a serial port), and then it populates data based on that barcode.
The scanning and populating part is no problem. My issue is as soon as I open this dialog, I start to read from the serial port to see if anything has been scanned. This freezes the GUI and doesn't allow the user to cancel. So after a bit of research, it looks like I want to do some threading, but I can't figure it out.
Here's what I have:
...
self.edit_location = EditLocation(self)
self.edit_location.show()
self.edit_location.wait_for_scan()
class EditLocation(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.ui = ui_edit_location.Ui_EditLocation()
self.ui.setupUi(self)
self.parent = parent
def wait_for_scan(self):
thread = ScanThread()
thread.start()
def receivedBarcode(self, barcode):
print "recieved", barcode
class ScanThread(QThread):
def __init__(self):
QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
barcode = scan_barcode()
self.emit(SIGNAL('receivedBarcode'), barcode)
and in my scanning file... (uses pyBarcode)
s = serial.Serial(scanner_port)
def scan_barcode():
return s.readline()
The GUI still freezes until a barcode is scanned. Any help would be much appreciated!