针对类实现QProgressBar

时间:2014-08-30 14:26:52

标签: python qt parsing python-2.7 pyqt4

我的PyQt程序有两个小部件(选择文件等),然后是一个显示已解析文件结果的主窗口。

该程序适用于小样本文件,但在尝试解析较大的文件时,它会挂起(显示"无响应"),然后在大约30秒左右后显示结果。

我想在主窗口打开之前实现QDialog。 QDialog将有一个进度条,让用户知道主窗口何时打开。

此进度条需要设置为弹出主窗口之前的时间长度。

实现此目的的最佳方法是什么?我已经看到了一些示例,但进度条只设置为标准化时间,而不是处理(解析)完成时。

我目前有以下代码打开主窗口。

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

此窗口的所有处理在打开时完成。那么如何连接QProgressBar?

2 个答案:

答案 0 :(得分:1)

将你持久的过程放在某种线程中。阅读本文:http://qt-project.org/doc/qt-5/threads-technologies.html 从该线程发出信号以更新进度条。这样您的应用程序就不会挂起,用户会看到进度。

但是,由您的加载例程决定在进度条中显示的百分比。如果您无法计算确切的百分比,请尝试某种估算(例如,根据文件的大小与文件的处理量)。

答案 1 :(得分:0)

首先,实现此目的的最佳方法是,您必须估算加载进度文件。接下来,使用QtCore.QThread实现它以创建后台进程。最后,将您的回复进度放入QtGui.QMainWindow

小例子;

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_())

QtCore.QThread的更多信息(建议阅读以理解行为)