It worked the first time but not again when the function that loops around processing data is re-called. I have 2 python programs. The main shows the progress bar and the second loops around processing data. I've researched all over stackoverflow and google and tried so hard to find a solution. The closest solution was question asked 'unexplained delay after QProgressBar finish loading 2' but I'm unable to apply it to my problem. I also tried applying QApplication.processEvents() but I was not successful.
Main program... etc...
def connectDevice(self) if self.usb_serial != None: self.ui.ProgressBar.setMinimum(0) # settings for showing pyqt4 progress bar self.ui.ProgressBar.setMaximum(0) # not indicating as expected self.ui.ProgressBar.setValue(0) # first showing 0% self.device = ScopeDev(self.usb_serial) # device.py my second Python program self.device.start() # Connect Call... class ScopeDev/def run(self): self.device.init_received.connect(self.init_received) # end of data processing signal received else: self.Display_MSG("Connection Error", "device not plug into USB port." ) def reprocessdata(self): logging.info("Re-Processing Data...") self.ui.ProgressBar.setMaximum(0) # hoping to kick off the progress bar again. Not even showing 0% self.ui.ProgressBar.setValue(0) # I tried insert QApplication.processEvents() here but did not work self.device.init() # Call class ScopeDev/def init(self): data was being processed def init_received(self): logging.debug("Init received") self.ui.ProgressBar.setMaximum(1) # indicated 100% on both times, when data processing completed self.ui.ProgressBar.setValue(1) # first from connectDevice and second time from reprocessdata
My second python program... etc...
class ScopeDev (QtCore.QThread): init_received = QtCore.pyqtSignal() def __init__(self, usb_serial, usb_serial_baud=9600, timeout=2): QtCore.QThread.__init__(self, None) self.serial = serial.Serial(usb_serial, usb_serial_baud, timeout=timeout) # connect to Arduino logging.debug("Connected (%s)" % usb_serial) def run(self): self.init() #1 Call...def init(self): def init(self): self.serial.readline().rstrip() # read println put out by Arduino/plaser.ino self.serial.write(b'init') self.sread(expect=b'^done_init$') def sread(self, expect=b'^cmd$'): # loops around to process data from Arduino...etc. when completed... self.init_received.emit() # emits the outbound signal back to the main
Asked
Active
Viewed 1,051 times
1

Gary Hughes
- 4,400
- 1
- 26
- 40

Richard Lang
- 11
- 4
-
You need a `QTimer` to update the gui every X seconds – JBernardo Apr 22 '13 at 23:54
-
Default [QThread::run](http://qt-project.org/doc/qt-4.8/qthread.html#run) calls `exec()`, which in turn enters the event loop. And your reimplementation of `run` lacks this, so your thread doesn't have the event loop, and hence no signals or slots. See [this](http://blog.qt.digia.com/blog/2006/12/04/threading-without-the-headache/) article. – gatto Apr 23 '13 at 00:17
-
Thanks JBernardo. My progressbar is set to range(0, 0). So it will always indicate while the function sread() is looping in my second program. I'm not updating the bar. setvalue(0). – Richard Lang Apr 23 '13 at 01:28
-
Thanks gatto, It's indicating in between start() and the signal received back from 2nd program thread first time. But not working when after in the function reprocessdata(). Do I've to .exit() terminate() or quit() the last tread after the first run, and then start() again? I thought by emmiting at the end of the loop in function sread() in 2nd program that the thread ends? I'm really having difficulty here, even after reading all the class references and trying to understand threading? – Richard Lang Apr 23 '13 at 01:41
-
I keep trying and no success, bar is indicating during the first data processing run and the GUI is responsive. But on the second and subsequent data re-processing, the GUI bar is frozen, (Not Responding) – Richard Lang Apr 23 '13 at 04:21
-
I finally got it to work! Thanks to all. I inserted a serial.close() in function sread after self.init_received.emit() and when re-processing data I repeat the connectDevice again and self.device.start() to re-implement the run(). However this has caused a different problem in that my programming flow via serial communication has been disrupted by opening and closing the port. I'm also going to try updating the GUI with QTimer every X seconds. – Richard Lang Apr 24 '13 at 01:24
1 Answers
0
Here are some sugestion:
First of all remove first initialization, this 3 lines:
self.ui.ProgressBar.setMinimum(0) # settings for showing pyqt4 progress bar
self.ui.ProgressBar.setMaximum(0) # not indicating as expected
self.ui.ProgressBar.setValue(0) # first showing 0%
that is done by default.
Second thing to do: in reprocessdata
try to switch those two lines:
self.ui.ProgressBar.setMaximum(0)
self.ui.ProgressBar.setValue(0)
3. Instead with previous two lines, try to reset progress bar with:
void QProgressBar::reset () [slot]
Reset the progress bar. The progress bar "rewinds" and shows no progress.
in your case: self.ui.ProgressBar.reset()
- what I don't understand is that why you use progress bar? because you have only two states - 0 (0%) and 1 (100%)

Aleksandar
- 3,541
- 4
- 34
- 57