使用QThread的PyQt4文件复制进度条

时间:2015-01-20 16:50:52

标签: python pyqt4 qthread

我正在创建一个需要多个文件复制和大文件复制操作的应用程序。之前我使用python线程和QtGui.qApp.processEvents()来使GUI响应。现在我想转移到QThread以获得更好的线程处理。这是我计划实现的模拟代码。有些功能并不完整,但概念就在这里。基本上我想将大文件复制到外部驱动器并计算/监控外部驱动器的可用大小以更新进度条。以下是使用Qt Designer ui_progress.py 生成的代码: -

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'progress.ui'
#
# Created: Tue Jan 20 21:19:13 2015
#      by: PyQt4 UI code generator 4.10.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 100)
        self.gridLayout_2 = QtGui.QGridLayout(Dialog)
        self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setObjectName(_fromUtf8("label"))
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
        self.progressBar = QtGui.QProgressBar(Dialog)
        self.progressBar.setProperty("value", 24)
        self.progressBar.setObjectName(_fromUtf8("progressBar"))
        self.gridLayout_2.addWidget(self.progressBar, 1, 0, 1, 1)
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.gridLayout_2.addWidget(self.pushButton, 2, 0, 1, 1)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "File copy info here.", None))
        self.pushButton.setText(_translate("Dialog", "Start", None))

这是我的 test.py 来模拟文件复制操作。在这里,我没有使用外部磁盘的大小,但是为了检查逻辑而去了数字范围: -

from PyQt4 import QtCore
from PyQt4 import QtGui
from gui.ui_progress import Ui_Dialog
import sys
import time, os
import shutil


source = "path/to/my/large/file"
dest = "path/to/destination/directory"

class ProgessGui(QtGui.QDialog, Ui_Dialog):
    global source
    global dest

    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.pushButton.clicked.connect(self.test)

    def get_zize(self, path):
        return os.path.getsize(path)

    def copy_file(self, source, dest):
        # I keep all my multiple copy operation in this function.
        try:
            shutil.copy(source, dest)
            if some_condition:
              #copy other file
        except:
            print "File copy failed..."

    def test(self):
        self.ui.progressBar.setValue(0)
        self.copy_file(source, dest)
        self.workThread = WorkThread()
        self.workThread.start()


class WorkThread(QtCore.QThread):

    def __init__(self):

        QtCore.QThread.__init__(self)

    def __del__(self):
        self.wait()

    def run(self):
        while self.alive:
            for i in range(20):
                time.sleep(0.13)
                self.emit(QtCore.SIGNAL("progress(int)"), i)
        return

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = ProgessGui()
    myapp.show()
    sys.exit(app.exec_())

当我运行上面的代码时,progressbar设置为0但我收到以下错误: -

self.workThread = WorkThread()
NameError: global name 'WorkThread' is not defined

我想我在这里遇到两个问题。任何帮助表示赞赏。

修改1

更正了上面的代码。现在错误消息已经消失。但进度条设置为0%而不是前进。

0 个答案:

没有答案