我正在使用 PySide 制作一个小型 FTP 客户端,并且我正在尝试在下载文件时更新进度条。我告诉 qftp 对象监听 dataTransferProgress 信号,以便在取得进展时调用一个方法来设置 progressBar 的值。但是,每次调用该方法时,实际存在的文件的最大值为 -1。有什么我做错了吗?
资源:
import os
import platform
import sys
import threading
from ftplib import FTP
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtNetwork import *
import ui_FTPWindow
class FtpWindow(QDialog, ui_FTPWindow.Ui_Dialog):
def __init__(self, parent=None):
super(FtpWindow, self).__init__(parent)
self.connected = False
self.username = None
#self.fileTree.itemActivated.connect(self.processItem)
#self.fileTree.currentItemChanged.connect(self.enableDownloadButton)
self.password = None
self.server = None
self.port = None
self.ftp = None
self.qftp = QFtp(self)
self.url = None
self.folderIcon = None
self.fileIcon = None
self.currentDir = None
self.count = 0
#self.fileTree.setEnabled(False)
self.setupUi(self)
self.connectButton.clicked.connect(self.connectOrDisconnect)
self.mkdirButton.clicked.connect(self.makedir)
self.downloadButton.clicked.connect(self.download)
self.updateUi()
def updateUi(self):
self.serverEdit.setText('ftpserverhere')
self.progressBar.setValue(0);
if self.serverEdit.text() is not None:
self.connectButton.setEnabled(True)
else:
self.connectButton.setEnabled(False)
def populateTree(self):
self.fileTree.clear()
self.consoleLabel.setText("Loading file tree..")
root = QTreeWidgetItem(self.ftp.pwd())
self.fileTree.addTopLevelItem(root)
for item in self.ftp.nlst():
child = QTreeWidgetItem()
child.setText(0, item)
root.addChild(child)
root.setExpanded(True)
if not "." in item:
self.count = 1
self.goDeeper(item, child)
print item
def connectOrDisconnect(self):
self.url = QUrl(self.serverEdit.text());
self.qftp = QFtp(self)
self.qftp.commandFinished.connect(self.commandFinished)
self.qftp.dataTransferProgress.connect(self.updateDataTransferProgress)
print "ftp listeners set"
if "ftp" in self.url.scheme().lower():
if self.url.userName():
self.ftp = FTP(self.url.host(), self.url.userName(), self.url.password())
self.currentDir = self.ftp.nlst()
self.qftp.connectToHost(self.url.host())
print self.qftp.currentCommand()
def goDeeper(self, s, parent):
for item in self.ftp.nlst(s):
if item is None:
return
deeperChild = QTreeWidgetItem()
deeperChild.setText(0, item)
parent.addChild(deeperChild)
if not "." in item:
self.goDeeper(item, deeperChild)
def download(self):
fileName = self.fileTree.currentItem().text(0)
if QFile.exists(fileName):
QMessageBox.information(self, "FTP",
"There already exists a file called %s in the current "
"directory." % fileName)
return
self.outFile = QFile(fileName)
if not self.outFile.open(QIODevice.WriteOnly):
QMessageBox.information(self, "FTP",
"Unable to save the file %s: %s." % (fileName, self.outFile.errorString()))
self.outFile = None
return
self.qftp.get(self.fileTree.currentItem().text(0), self.outFile)
self.consoleLabel.setText("Downloading %s..." % fileName)
self.downloadButton.setEnabled(False)
def updateDataTransferProgress(self, readBytes, totalBytes):
self.progressBar.setMaximum(totalBytes)
self.progressBar.setValue(readBytes)
print self.qftp.currentCommand()
print totalBytes
print readBytes
def makedir(self):
foldername = unicode(self.commandEdit.text())
if self.commandEdit.text() != "":
#self.qftp.rawCommand(unicode("mkdir " + foldername))
self.qftp.mkdir(foldername )
print self.qftp.currentCommand()
self.consoleLabel.setText("Creating directory %s" % (foldername))
def executeCommand(self):
if self.commandEdit.text() == "":
return
else:
command = self.commandEdit.text()
self.qftp.rawCommand(command)
def commandFinished(self, error):
if str(self.qftp.currentCommand()) == 'PySide.QtNetwork.QFtp.Command.Mkdir':
self.populateTree()
self.consoleLabel.setText("Directory %s created" % self.commandEdit.text())
self.commandEdit.setText("")
if str(self.qftp.currentCommand()) == 'PySide.QtNetwork.QFtp.Command.ConnectToHost':
self.qftp.login(unicode(self.url.userName), unicode(self.url.password))
self.consoleLabel.setText("Connected to server: %s" % self.url.host())
self.qftp.get('')
print "Invisible file got"
print "completed"
if str(self.qftp.currentCommand()) == 'PySide.QtNetwork.QFtp.Command.Login':
self.populateTree()
self.consoleLabel.setText("Logged in as %s" % self.url.userName())
if str(self.qftp.currentCommand()) == 'Pyside.QtNetwork.QFtp.Get':
if error:
self.consoleLabel.setText("Canceled download of %s." % self.outFile.fileName())
self.outFile.close()
self.outFile.remove()
else:
self.consoleLabel.setText("Downloaded %s to current directory." % self.outFile.fileName())
self.outFile.close()
def main():
app = QApplication(sys.argv)
app.setOrganizationName("Qtrac Ltd.")
app.setOrganizationDomain("qtrac.eu")
app.setApplicationName("Image Changer")
form = FtpWindow()
form.show()
app.exec_()
main()
非常感谢!