QNetworkAccessManager - finished signal never triggers the slot



  • I'm trying to use QNetworkAcessManager to get the source of a url .. But it seems that there's a problem with the signal-slot complex!

    my onFinished() is never triggered! Why?

    @void Worker::start(QString url)
    {
    QNetworkAccessManager *manager = new QNetworkAccessManager();
    QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(url)));
    QObject::connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
    QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
    this, SLOT(onError(QNetworkReply::NetworkError)));
    }

    void Worker::onFinished()
    {
    qDebug() << "Hell yeah! slot has been triggered finally!";
    }

    void Worker::onError(QNetworkReply::NetworkError error)
    {
    qDebug() << "Error!";
    }@


  • Lifetime Qt Champion

    Hi,

    You should also connect the error signal, you might be having something going wrong somewhere else.



  • [quote author="SGaist" date="1406406722"]Hi,

    You should also connect the error signal, you might be having something going wrong somewhere else.[/quote]

    Actually, I've done that as shown in the first post ( edited ), but also nothing happens at all!


  • Lifetime Qt Champion

    What does the url look like ?



  • [quote author="SGaist" date="1406410505"]What does the url look like ?[/quote]

    I've tried everything actually, like for example:
    http://www.google.com
    http://qt-project.org
    http://cplusplus.com
    http://microsoft.com

    So it's hopefully not a url-related problem. :)



  • Ok can you guys just give me a working piece of code? regardless of helping me with my specific problem, because I completely believe now that it's unsolvable! :(


  • Lifetime Qt Champion

    Your code is fine, except that you don't need to create a QNetworkAccessManager for each request. Just make it a member variable and use the same manager for all your request.

    You have code examples using QNetworkAccessManager directly in the documentation.

    Did you check that the network is up and running ?



  • [quote author="SGaist" date="1406494121"]Your code is fine, except that you don't need to create a QNetworkAccessManager for each request. Just make it a member variable and use the same manager for all your request.

    You have code examples using QNetworkAccessManager directly in the documentation.

    Did you check that the network is up and running ?[/quote]
    I've also tried this:
    @ QNetworkAccessManager manager;
    QNetworkRequest request;
    request.setUrl(QUrl(url));
    request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
    QNetworkReply *response = manager.get(request);
    connect(response, SIGNAL(finished()), this, SLOT(onFinished()));@
    But it doesn't work also, I checked every example & also searched all over the web, every asynchronous code won't work!

    I've made it synchronously, but I do need it asynchronously!

    The network connection is up & running, unless it'd fire up the error signal.

    :(


  • Lifetime Qt Champion

    That last version won't work since manager get's out of scope at the end of the function call.

    Do you have a QCoreApplication event loop running ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.