2014-08-30 64 views
0

我的PyQt程序有2個小部件(選擇文件等),然後是一個顯示解析文件結果的主窗口。針對類實現QProgressBar

該程序適用於小樣本文件,但當試圖解析較大的文件時,它會掛起(顯示「Not Responding」),然後在大約30秒左右後顯示結果。

我想在主窗口打開之前實現一個QDialog。 QDialog將有一個進度條讓用戶知道主窗口何時打開。

此進度條需要設置爲主窗口彈出之前的時間長度。

實現此目的的最佳方法是什麼?我已經看到了一些示例,但進度條只是設置爲標準化時間,而不是處理(解析)完成時。

我目前有以下代碼打開主窗口。

def openWidgetMain(self): 
     self.WidgetMain = WidgetMain() 
     self.WidgetMain.show() 
     self.close() 

此窗口的所有處理都在打開時完成。那麼如何連接QProgressBar?

回答

0

首先,最好的方法來實現這一點,你必須估計你的加載進度文件。接下來,用QtCore.QThread執行它來創建後臺進程。最後,把你的回撥進度放到你的QtGui.QMainWindow

小例子;的QtCore.QThread

import sys 
import time 
from PyQt4 import QtGui 
from PyQt4 import QtCore 

class QCustomThread (QtCore.QThread): 
    startLoad = QtCore.pyqtSignal(int) 
    progressLoad = QtCore.pyqtSignal(int) 
    statusLoad = QtCore.pyqtSignal(bool) 

    def __init__ (self, parentQWidget = None): 
     super(QCustomThread, self).__init__(parentQWidget) 
     self.wasCanceled = False 

    def run (self): 
     # Simulate data load estimation 
     numberOfprogress = 100 
     self.startLoad.emit(numberOfprogress) 
     for progress in range(numberOfprogress + 1): 
      # Delay 
      time.sleep(0.1) 
      if not self.wasCanceled: 
       self.progressLoad.emit(progress) 
      else: 
       break 
     self.statusLoad.emit(True if progress == numberOfprogress else False) 
     self.exit(0) 

    def cancel (self): 
     self.wasCanceled = True 

class QCustomMainWindow (QtGui.QMainWindow): 
    def __init__ (self): 
     super(QCustomMainWindow, self).__init__() 
     # Create action with QPushButton 
     self.startQPushButton = QtGui.QPushButton('START') 
     self.startQPushButton.released.connect(self.startWork) 
     self.setCentralWidget(self.startQPushButton) 
     # Create QProgressDialog 
     self.loadingQProgressDialog = QtGui.QProgressDialog(self) 
     self.loadingQProgressDialog.setLabelText('Loading') 
     self.loadingQProgressDialog.setCancelButtonText('Cancel') 
     self.loadingQProgressDialog.setWindowModality(QtCore.Qt.WindowModal) 

    def startWork (self): 
     myQCustomThread = QCustomThread(self) 
     def startLoadCallBack (numberOfprogress): 
      self.loadingQProgressDialog.setMinimum(0) 
      self.loadingQProgressDialog.setMaximum(numberOfprogress) 
      self.loadingQProgressDialog.show() 
     def progressLoadCallBack (progress): 
      self.loadingQProgressDialog.setValue(progress) 
     def statusLoadCallBack (flag): 
      print 'SUCCESSFUL' if flag else 'FAILED' 
     myQCustomThread.startLoad.connect(startLoadCallBack) 
     myQCustomThread.progressLoad.connect(progressLoadCallBack) 
     myQCustomThread.statusLoad.connect(statusLoadCallBack) 
     self.loadingQProgressDialog.canceled.connect(myQCustomThread.cancel) 
     myQCustomThread.start() 

myQApplication = QtGui.QApplication(sys.argv) 
myQCustomMainWindow = QCustomMainWindow() 
myQCustomMainWindow.show() 
sys.exit(myQApplication.exec_()) 

的介紹:(推薦閱讀理解的行爲)

+1

感謝。現在將讀取QThread類 – MPythonLearner 2014-08-30 17:21:53

1

把你的持久過程放在某種線程中。閱讀:http://qt-project.org/doc/qt-5/threads-technologies.html 從該線程發出一個信號來更新你的進度條。這樣你的應用程序就不會掛起,用戶可以看到進度。

但是,由您的加載例程決定在進度條中顯示哪個百分比。如果您無法計算確切的百分比,請嘗試某種估算(例如,根據文件的大小與文件的處理量)。