I am working on a project in which I need to download and parse an XML file from a network location. I’ve been trying to use the QtNetwork module to accomplish this, but I’m running into a seemingly simple problem that I’ve spent many hours trying to solve. My code is as follows:
class NetworkAccessor(QObject):
done = False
def importXml(self):
self.manager = QNetworkAccessManager()
self.manager.finished.connect(self.fileReady(QNetworkReply))
self.manager.get(QNetworkRequest(QUrl("http://192.168.5.243/details.xml")))
def fileReady(self, response):
self.f = QTemporaryFile()
print type(response)
self.f.write(response.readAll())
self.done = True
print "Done"
When I instantiate the NetworkAccessor class and call importXml, I get the following error:
Traceback (most recent call last):
File "C:/SVN-Local/Thermal/PyQtTestTemplate.py", line 40, in updateUi
f = networkAccessor.importXml()
File "C:/SVN-Local/Thermal/PyQtTestTemplate.py", line 14, in importXml
self.connect(self.manager,SIGNAL("finished(QNetworkReply*)"),self.fileReady(QNetworkReply))
File "C:/SVN-Local/Thermal/PyQtTestTemplate.py", line 20, in fileReady
self.f.write(response.readAll())
TypeError: QIODevice.readAll(): first argument of unbound method must have type 'QIODevice'
It seems to indicate that the argument passed to the fileReady method is not instantiated. Furthermore, the print type(response) statement above indicates that response is of type PyQt4.QtNetwork.QNetworkReply.
I’ve tried various ways of connecting the signal to the slot including the “old fashioned” way: self.connect(self.manager,SIGNAL("finished(QNetworkReply*)"),self.fileReady("QNetworkReply")) as well as using short-circuit parameter passing with no success.
Can somebody show me the proper way to pass an instantiated instance of a QNetworkReply object from a signal to a slot?
Thanks.
Just pass it the slot, the QNetworkReply object will get passed through by the signal: