针对一个类实现QProgressBar [英] Implementing QProgressBar against a class

查看:59
本文介绍了针对一个类实现QProgressBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My PyQt program has 2 widgets (selecting files etc) and then a Main Window which displays the results of the parsed files.

该程序非常适合小的示例文件,但是当尝试解析较大的文件时,它将挂起(显示无响应"),然后在大约30秒左右后显示结果.

The program works great for small sample files, but when trying to parse larger files it will hang (display "Not Responding") and then show the results after about 30 seconds or so.

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

I would like to implement a QDialog before the Main Window opens. The QDialog will have a progress bar to let the user know when the Main Window will open.

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

This progress bar needs to be set to the length of time before the Main Window pops up.

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

What is the best way to implement this? I have seen some examples, but the progress bar is just set to a standardised time, not when the processing(parsing) is complete.

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

I currently have the following code which opens the Main Window.

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

此窗口打开时,将完成所有处理.那么如何连接QProgressBar?

All the processing for this window is done when it opens. So how do I connect the QProgressBar?

推荐答案

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

First, best way to implement this, Your must estimate your load progress file. Next, implement it with QtCore.QThread to create background process. Last, put your call back progress into your 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 的更多信息(推荐阅读以了解行为)

More infomation of QtCore.QThread (Recommend read to understand behavior)

这篇关于针对一个类实现QProgressBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆