I'm trying to download something in a QThread. I have been reading and I know that I should use an event loop in order<br>to get signals/slots working. exec_() seems to be the thing that I need, but it doesn't work as downloadFinished() will <br>
never be called. Can I get some help, please?<br><br>Here is the code:<br><br>class Async(QThread):<br> def run(self):<br> self.jar = QNetworkCookieJar()<br> self.manager = QNetworkAccessManager()<br> self.manager.setCookieJar(self.jar)<br>
self.manager.finished.connect(self.downloadFinished)<br> self.manager.authenticationRequired.connect(self.authenticationRequired)<br> self.request = QNetworkRequest(self.url)<br> self.request.setRawHeader("User-Agent", self.userAgent)<br>
self.reply = self.manager.get(self.request)<br> self.reply.downloadProgress.connect(self.progressCallback)<br> self.exec_()<br><br> def download(self, url, userAgent = "Wget/1.12 (linux-gnu)", user = "", password = ""):<br>
self.url = QUrl(url)<br> self.userAgent = userAgent<br> self.user = user<br> self.password = password<br> self.returnData = None<br> self.start()<br><br> def authenticationRequired(self, reply, authenticator):<br>
authenticator.setUser(self.user)<br> authenticator.setPassword(self.password)<br><br> def progressCallback(self, done, total):<br> self.emit(QtCore.SIGNAL('progress(int, int)'), done, total)<br><br> def downloadFinished(self, reply):<br>
self.redirect = reply.attribute(QNetworkRequest.RedirectionTargetAttribute).toUrl()<br> if not self.redirect.isEmpty():<br> self.request = QNetworkRequest(self.redirect)<br> self.request.setRawHeader("User-Agent", self.userAgent)<br>
self.reply = self.manager.get(self.request)<br> self.reply.downloadProgress.connect(self.progressCallback)<br> else:<br> self.emit(QtCore.SIGNAL('finished(PyQt_PyObject)'), str(self.reply.readAll()))<br>
self.quit()<br><br><br><br>Regards<br clear="all"><br>-- <br>Alexander Nestorov<br>