PyQt文件复制进度条

时间:2011-03-28 23:10:48

标签: pyqt

我正在尝试使用QProgressBar来显示PyQt中文件副本的进度。我已经看了一下,我已经使用QFile在一个线程中运行实际副本了。我只是不能让酒吧更新。

以下是一些代码:

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永远不会被调用,因此显然没有发出信号,但我不确定原因。

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

2 个答案:

答案 0 :(得分:1)

好的,这就是我提出的工作原理,但显然会降低文件复制性能。

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

答案 1 :(得分:0)

QFile不会发出bytesWritten:

  

与其他QIODevice不同   实现,例如QTcpSocket,   QFile不会发出   aboutToClose(),bytesWritten()或   readyRead()信号。这个   实现细节意味着QFile   不适合阅读和   写某些类型的文件,例如   作为Unix平台上的设备文件。

http://doc.qt.nokia.com/latest/qfile.html

你必须实现自己的循环并自己发出进展。