[PyQt] Problem with threaded ftp: Cannot queue arguments of type 'QUrlInfo'

Brian Knudson briank at pipelinefx.com
Fri Nov 16 22:40:10 GMT 2012


Certainly, I'm doing something wrong, but I must be missing it.

I've made a little demo script that downloads a file from ftp & updates a progress bar while doing so.  The downloading & updating the progress bar works fine, however, I'm trying to do the next step which is to take a url that looks like "ftp://foo.com/bar/*", in which case I would list the contents of the directory, then do a get on the returned files.

At the moment, I'm just trying to do a list on any directory & print the results to the command line.

When I try to do a listInfo.connect, I get an error message:

QObject::connect: Cannot queue arguments of type 'QUrlInfo'
(Make sure 'QUrlInfo' is registered using qRegisterMetaType().)

... as I understand it, qRegisterMetaType is not something that can be done in PyQt & is also a sign of a fundamental problem, and herein lies my problem.  I can do a commandFinished.connect and dataTransferProgress.connect without issue, but listInfo.connect doesn't seem to work (as I would expect it).

Any ideas how to correct this?

Here's some example code (pardon the length).  I would like to be able to print the listed files/urls from the function "lister".  Ultimately, I'd like to then have that function formulate new urls & pass them back to connectAndDownload to download each of the files (of course, this will require modifications to connectAndDownload, but we're not there yet).

#!/usr/bin/env python

from PyQt4 import QtCore, QtGui, QtNetwork

class FtpWorker(QtCore.QThread):
    dataTransferProgress = QtCore.pyqtSignal(int,int)
    def __init__(self,url,parent=None):
        super(FtpWorker,self).__init__(parent)
        self.ftp = None
        self.outFile = None
        self.get_index = -1

        self.url = url

    def run(self):
        self.connectAndDownload()
        self.exec_()

    def ftpCommandFinished(self, command_index, error):
        print "-----commandfinished-----",command_index
        
        if self.ftp.currentCommand == QtNetwork.QFtp.ConnectToHost:
            if error:
                QtGui.QMessageBox.information(self, "FTP",
                        "Unable to connect to the FTP server at %s. Please "
                        "check that the host name is correct.")
            return

        if self.ftp.currentCommand == QtNetwork.QFtp.Get or command_index == self.get_index:
            if error:
                print "closing outfile prematurely"
                self.outFile.close()
                self.outFile.remove()
            else:
                print "closed outfile normally"
                self.outFile.close()

            self.outFile = None

    def ftpDataTransferProgress(self,a,b):
        self.dataTransferProgress.emit(a,b)


    def lister(self,url_info):
        print url_info.name()


    def connectAndDownload(self):
        if self.ftp:
            self.ftp.abort()
            self.ftp.deleteLater()
            self.ftp = None
            return

        self.ftp = QtNetwork.QFtp()
        self.ftp.commandFinished.connect(self.ftpCommandFinished)
        self.ftp.listInfo.connect(self.lister)
        self.ftp.dataTransferProgress.connect(self.ftpDataTransferProgress)

        url = QtCore.QUrl(self.url)

        print "connect",self.ftp.connectToHost(url.host(), url.port(21))
        print "login",self.ftp.login(url.userName(), url.password())

        # if url.path():
        #     self.ftp.cd(url.path())

        print "Connecting to FTP server %s..." % str(url.host())

        #-----------------------------------------------------------------
        import os
        fileName = os.path.basename(self.url)

        if QtCore.QFile.exists(fileName):
            print "removing '%s'" % fileName
            os.unlink(fileName)

        self.outFile = QtCore.QFile(fileName)
        if not self.outFile.open(QtCore.QIODevice.WriteOnly):
            QtGui.QMessageBox.information(self, "FTP",
                    "Unable to save the file %s: %s." % (fileName, self.outFile.errorString()))
            self.outFile = None
            return

        tmp = self.ftp.list()
        print "starting list",tmp

        print "ftp.get(%s,%s)" % (str(url.path()), self.outFile)
        self.get_index = self.ftp.get(url.path(), self.outFile)
        
        #-----------------------------------------------------------------



class AddProgresWin(QtGui.QWidget):
    def __init__(self, parent=None):
        super(AddProgresWin, self).__init__(parent)
        
        self.thread = FtpWorker(url="ftp://ftp.qt.nokia.com/developerguides/qteffects/screenshot.png")

        self.thread.dataTransferProgress.connect(self.updateDataTransferProgress)

        self.nameLabel = QtGui.QLabel("0.0%")
        self.nameLine = QtGui.QLineEdit()
        
        self.progressbar = QtGui.QProgressBar()
        
        mainLayout = QtGui.QGridLayout()
        mainLayout.addWidget(self.progressbar, 0, 0)
        mainLayout.addWidget(self.nameLabel, 0, 1)
        
        self.setLayout(mainLayout)
        self.setWindowTitle("Processing")
        
        self.thread.start()
        
    def updateDataTransferProgress(self, readBytes, totalBytes):
        self.progressbar.setMaximum(totalBytes)
        self.progressbar.setValue(readBytes)
        perct = "%2.1f%%" % (float(readBytes)/float(totalBytes)*100.0)
        self.nameLabel.setText(perct)


if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.path)

    pbarwin = AddProgresWin()
    pbarwin.show()

    sys.exit(app.exec_())



More information about the PyQt mailing list