0

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!

jongusmoe
  • 676
  • 4
  • 16
  • 1
    Retain a reference to `thread`. I'm not sure why you need to do this exactly, but I would speculate that as python is trying to garbage collect the `thread` variable it gets stuck on the `self.wait()` call in the `__del__` function. – hackyday Jun 11 '14 at 22:34
  • Thank you! I did that, and I also passed in 'parent' to the QThread and did `QThread.__init__(self, parent)` and that fixed it. Appreciate the help! – jongusmoe Jun 12 '14 at 12:50

0 Answers0