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 解决方案,所以任何帮助都会很棒。
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.
Here's some code:
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 never gets called so the signal obviously isn't being emitted but I'm not sure why.
I've searched high and low on this but haven't found a good PyQt solution for this so any help would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,这就是我想出的方法,它有效,但显然会降低文件复制性能。
Ok so here's what I came up with which works but obviously slows down the file copy performance.
QFile 不发出 bytesWritten:
http://doc.qt.nokia.com/latest/qfile.html
您必须实现自己的循环并自己发出进度。
QFile does not emit bytesWritten:
http://doc.qt.nokia.com/latest/qfile.html
You will have to implement your own loop and emit the progress yourself.