Application hangs when using QNetworkAccessManager

  • Hi, I am trying to check for an internet connection using the following code.
    The problem I am facing is that whenever application is executed my antivirus prompts whether to allow the application to access the internet or not, if its allowed then its work properly, incase if there is no internet connection then the respective error message is shown, but in case if the application is not allowed to access the ineternet then my application hangs for around 3mins and after 3mins start responding again. what could be the possible solution for this, any kind of help will be appreciated.

    @mNetworkManager = new QNetworkAccessManager(this);
    url = "";
    reply = mNetworkManager->get(QNetworkRequest(url));
    QObject::connect(mNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onNetworkReply(QNetworkReply*)));

    QString myapp::onNetworkReply(QNetworkReply* reply)

    QString replyString;
    QFile file("temp.txt");
    // | QIODevice::Text);
    QDataStream f1(&file);

    if(reply->error() == QNetworkReply::NoError)
    int httpstatuscode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toUInt();

        case RESPONSE_OK:
        if (reply->isReadable())
            //Assuming this is a human readable file replyString now contains the file
            replyString = QString::fromUtf8(reply->readAll().data());
            if (
                f1 << reply->readAll();
        case RESPONSE_ERROR:
        QMessageBox::warning(this,"Error","There was an error in your internet connection",QMessageBox::Ok);
        case RESPONSE_BLOCKED:

    else if(reply->error() != QNetworkReply::NoError)
    QMessageBox::warning(this,"Error","No internet connection",QMessageBox::Ok);

    return replyString;



  • Have you tried halting your application using the debugger the find out where it actually stalls?

  • No,
    but i guess its due to the reason that request to the url was not proceeded as the application was blocked from accessing the internet.

  • Well, I do not consider "guessing" a valid debugging technique.

    What Qt version do you use? 4.8 got a threaded QNetworkAccessManager, so even though your request blocks your main thread shouldn't be affected.

  • I am using Qt 4.7.0

  • QNetworkAccessManager should not block, irregardless of the threaded and non-threaded version, and this is most probably a side effect of the desktop firewall (which are known to do more harm than good). As far as I can tell right now you are basically left with three options:

    • upgrade to 4.8
    • use your own threaded mechanism
    • debug your application to find the actual cause so we can find a solution

Log in to reply