简体   繁体   中英

Pyqt4 - QProgressBar how to indicate everytime while a function loops around processing data

  1. 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 
  2. 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 

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%)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM