PyQt 文件复制进度条 [英] PyQt file copy progress bar

查看:78
本文介绍了PyQt 文件复制进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 QProgressBar 来显示 PyQt 中文件复制的进度.我已经环顾四周,并且已经使用 QFile 在线程中运行实际副本.我只是无法让栏更新.

I am trying to get a QProgressBar to show the progress of a file copy in PyQt. I've looked about and I've got as far as running the actual copy in a thread using QFile. I just can't get the bar to update though.

这是一些代码:

class ProgressDialog(QtGui.QDialog):

    def __init__(self, parent, source, destination):
        QtGui.QDialog.__init__(self, parent)
        self.parent = parent
        self.source = source
        self.destination = destination

        self.add_diag = progress_diag.Ui_Dialog()
        self.add_diag.setupUi(self)

        self.add_diag.infoLabel.setText("Copying: %s" % (self.source))
        self.sourcefile = QtCore.QFile(self.source)

        self.add_diag.progressBar.setMinimum(0)
        self.add_diag.progressBar.setMaximum(self.sourcefile.size()/1024)

        self.written = 0

        self.show()
        self.copy()

    def copy(self):

        copy_thread = CopyThread(self, self.sourcefile, self.destination)
        self.connect(copy_thread.destination_file, QtCore.SIGNAL("bytesWritten(qint64)"), self.update_progress)
        copy_thread.procDone.connect(self.finished_copy)
        copy_thread.start()

    def update_progress(self, progress):
        print "Working", progress
        self.written += progress
        self.add_diag.progressBar.setValue(written/1024)

    def finished_copy(self, state):
        self.close()

class CopyThread(QtCore.QThread):

    procDone = QtCore.pyqtSignal(bool)

    def __init__(self, parent, source, destination):
        QtCore.QThread.__init__(self, parent)
        self.source = source
        self.destination_file = QtCore.QFile(destination)

    def run(self):
        self.source.copy(self.destination_file.fileName())
        self.procDone.emit(True)

update_progess 永远不会被调用,所以信号显然没有被发出,但我不知道为什么.

update_progess never gets called so the signal obviously isn't being emitted but I'm not sure why.

我对此进行了高低搜索,但还没有找到一个好的 PyQt 解决方案,所以任何帮助都会很棒.

I've searched high and low on this but haven't found a good PyQt solution for this so any help would be great.

推荐答案

好的,这就是我想出的方法,它可以工作但显然会降低文件复制性能.

Ok so here's what I came up with which works but obviously slows down the file copy performance.

class ProgressDialog(QtGui.QDialog):

    def __init__(self, parent, source, destination):
        QtGui.QDialog.__init__(self, parent)
        self.parent = parent
        self.source = source
        self.destination = destination
        self.add_diag = progress_diag.Ui_Dialog()
        self.add_diag.setupUi(self)

        self.add_diag.infoLabel.setText("Copying: %s" % (self.source))

        self.add_diag.progressBar.setMinimum(0)
        self.add_diag.progressBar.setMaximum(100)
        self.add_diag.progressBar.setValue(0)

        self.show()
        self.copy()

    def copy(self):

        copy_thread = CopyThread(self, self.source, self.destination)
        copy_thread.procPartDone.connect(self.update_progress)
        copy_thread.procDone.connect(self.finished_copy)
        copy_thread.start()

    def update_progress(self, progress):
        self.add_diag.progressBar.setValue(progress)

    def finished_copy(self, state):
        self.close()

class CopyThread(QtCore.QThread):

    procDone = QtCore.pyqtSignal(bool)
    procPartDone = QtCore.pyqtSignal(int)

    def __init__(self, parent, source, destination):
        QtCore.QThread.__init__(self, parent)
        self.source = source
        self.destination = destination

    def run(self):
        self.copy()
        self.procDone.emit(True)

    def copy(self):
        source_size = os.stat(self.source).st_size
        copied = 0
        source = open(self.source, "rb")
        target = open(self.destination, "wb")

        while True:
            chunk = source.read(1024)
            if not chunk:
                break
            target.write(chunk)
            copied += len(chunk)
            self.procPartDone.emit(copied * 100 / source_size)

        source.close()
        target.close()

这篇关于PyQt 文件复制进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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