2013-04-22 33 views
1
  1. 它的工作第一次,但不會再當繞一圈處理數據的功能被重新調用。我有2個python程序。主要顯示進度條和處理數據的第二個循環。我已經研究了所有的stackoverflow和谷歌,並試圖很難找到一個解決方案。最接近的解決方案是在QProgressBar完成加載2後出現'原因不明的延遲'的問題,但我無法將其應用於我的問題。我也嘗試應用QApplication.processEvents(),但我沒有成功。PyQt4的 - QProgressBar如何指示,而每次的功能繞一圈處理數據

    主程序...等...

    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. 我的第二個Python程序...等等...

    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 
    
+0

你需要一個'QTimer'來每隔X秒更新一次gui – JBernardo 2013-04-22 23:54:22

+0

默認[QThread :: run](http:// qt-project .org/doc/qt-4.8/qthread.html#run)調用'exec()',然後進入事件循環。而'run'的重新實現缺少這個,所以你的線程沒有事件循環,因此沒有信號或插槽。請參閱[本](http://blog.qt.digia.com/blog/2006/12/04/threading-without-the-headache/)文章。 – gatto 2013-04-23 00:17:12

+0

謝謝JBernardo。我的進度欄設置爲範圍(0,0)。所以它會一直指示函數sread()在我的第二個程序中循環。我沒有更新酒吧。的setValue(0)。 – 2013-04-23 01:28:15

回答

0

這裏有一些sugestion:

第一個全部刪除第一次初始化,這3行:

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% 

這是默認完成的。

做的事:在reprocessdata嘗試切換這兩行:

self.ui.ProgressBar.setMaximum(0)   
    self.ui.ProgressBar.setValue(0) 

而是與前兩行,嘗試重置進度條帶:

void QProgressBar :: reset()[slot]

重置進度條。進度條「倒退」並顯示沒有 進度。

你的情況:self.ui.ProgressBar.reset()

-我不明白的是,爲什麼你使用進度條?因爲你只有兩個狀態 - 0(0%)和1(100%)

相關問題