[PyQt] QFtp.Put and the dataTransferProgress signal.

Anthony Plunkett anthony at thefort.org
Tue Nov 3 15:37:08 GMT 2009


Hi,

I've been trying to get a binary file uploaded to an FTP server using 
QtNetwork.QFtp on Windows XP.

The area I seem to be having trouble with is getting the 
dataTransferProgress signal to fire.  The method comes in two forms, one 
which accepts a QByteArray, which seemingly doesn't have the 
dataTransferProgress signal, and the other which requires a QIODevice 
which does.

I can manage to connect, authenticate and upload the file if I pass in 
the QFile.ReadAll() method, but since that returns a QByteArray, I don't 
get the lovely dataTransferProgress signal, which I want to use to tie 
to a progress bar.

I looked into just passing the QFile into the put method since it 
inherits QIODevice but that just causes the program to crash.  So I 
looked into the other children and QBuffer sounds like the right fit-- 
"The QBuffer class provides a QIODevice for a QByteArray".    Since I 
know the QFile.ReadAll() returns a QByteArray, I thought I may have 
cracked the problem, but when I try:

buffer = QtCore.QBuffer()
buffer.open(QtCore.QIODevice.ReadWrite)
buffer.setData(f.readAll())  #f is a QFile object.

then pass that buffer object into the QFtp.put(buffer,"outName") the 
program just crashes.

I'm sure I have a misunderstanding somewhere-- I can't find any Python 
examples of dataTransferProgress or QFtp.put that show me the correct 
usage, I've tried Google (and it's code search) and after a stint in the 
IRC channel, they pointed me here.  Attached is a minimal example (or as 
close to it as I could manage).  The commented out line 29 is the 
working example, but doesn't fire the signal (and I'm assuming because 
it's providing a QByteArray), line 28 should be handing the put method a 
QIODevice, and perhaps it is, but it's causing the program to crash ( 
which I've learnt tends to indicate I'm doing something rather silly).

Any help would be most appreciated.
-------------- next part --------------
import sys
from PyQt4 import QtCore, QtGui, QtNetwork
from PyQt4.QtSql import *

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.on_goUpload_clicked()
        
    def on_goUpload_clicked(self):
        self.client = QtNetwork.QFtp()
        self.connect(self.client, QtCore.SIGNAL('stateChanged(int)'),self.updateStatus)
        self.connect(self.client, QtCore.SIGNAL('dataTransferProgress(int,int)'),self.updateProgress)
        self.client.connectToHost('ftp.thefort.org')
        self.client.login("ting","moomin")
        
    def updateProgress(self, position, total):
        print(str(position) + "::" + str(total))
        
    def updateStatus(self,state):
        print(state)
        if state == 4:
            f= QtCore.QFile("binaryFile")
            f.open(QtCore.QIODevice.ReadOnly)
            buffer = QtCore.QBuffer()
            buffer.open(QtCore.QIODevice.ReadWrite)
            buffer.setData(f.readAll())
            self.client.put(buffer,"outname")  ## Causes a crash
            #self.client.put(f.readAll(),"outname")  ## Uploads, but no dataTransferProgress signal.

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


More information about the PyQt mailing list