I want to send http request in a QGIS plugin using PyQt. I found in this post working syntaxes to do so, but something odd occurs.

The script given in the question works both in the console and in my plugin. But the script given as an answer by German Carrillo, technically better, works when executed in QGIS python console, but doesn't as soon as it is integrated in a plugin.

So this works in the console, but doesn't when integrated in a plugin script :

from PyQt4.QtCore import QUrl
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest

def request_download(manager):
    url = QUrl("https://www.facebook.com")
    request = QNetworkRequest(url)
    manager.get( request )

def handle_download(reply):
    print reply.readAll()

manager = QNetworkAccessManager()
manager.finished.connect( handle_download )

Considering the logs I have put in my script to locate the issue, it appears the handle_dowload function is never called. So it means the QNetworkAccessManager doesn't send his finished signal.

Is there known reasons why something would work in the console but not in a plugin ?

  • shouldn't handle_download take a reply attribute?
    – adamczi
    Sep 22 '16 at 12:39
  • In a python point of vue, I find it weird too. Nevertheless, it works (in the console), and it is what German Carrillo gave as an answer in the post I linked. As the finished signal of QNetworkAccessManager returns a QNetworkReply, my guess is : for some Qt reason, the slot connected to this signal automatically takes the reply as an argument.
    – El Theo
    Sep 22 '16 at 12:44
  • that would be weird if you look at a simpler example of pushButton.clicked.connect(function) - if function is declared as function(self), additional argument won't fit here. where do you have these manager command in the plugin?
    – adamczi
    Sep 22 '16 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.