Check internet connection with QNetworkAccessManager question.



  • So i managed to check my internet connection with this way

    .pro
    @QT += network
    @

    .cpp

    @#include <QNetworkAccessManager>

    QNetworkAccessManager manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply
    )),
    this, SLOT(replyFinished(QNetworkReply*)));
    manager->get(QNetworkRequest(QUrl("http://google.com")));

    void MainWindow::replyFinished(QNetworkReply* reply)

    {

    if (reply->error() == QNetworkReply::NoError)
    
    
        qDebug("connected");
    
    else
    
        qDebug("error");
    
    delete reply;
    

    }@

    but what if i dont want to call another void? i was thinking something like
    @QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    manager->get(QNetworkRequest(QUrl("http://google.com")));
    while(manager.isRunning()){}
    //and here check the output of QNetworkReply...@

    So the question is.. what should i put instead of while(manager.isRunning)
    and what should i put instead of the comment above(//and here check the output of QNetworkReply)...


  • Moderators

    Here is what I did in QtWebService>
    @
    forever {
    if (qsm.isReplyReady()) {
    return qsm.replyRead();
    } else {
    qApp->processEvents(); // Ensures that application remains responive
    // to events (prevents freezing).
    }
    @

    You can see the whole code here: "link":https://gitorious.org/qwebservice/qwebservice/blobs/master/QWebService/sources/qwebmethod.cpp. Should be enough to check the body of ::isReplyReady().



  • [quote author="sierdzio" date="1343650001"]Here is what I did in QtWebService>
    @
    forever {
    if (qsm.isReplyReady()) {
    return qsm.replyRead();
    } else {
    qApp->processEvents(); // Ensures that application remains responive
    // to events (prevents freezing).
    }
    @

    You can see the whole code here: "link":https://gitorious.org/qwebservice/qwebservice/blobs/master/QWebService/sources/qwebmethod.cpp. Should be enough to check the body of ::isReplyReady().[/quote]

    Well i can see that you also connect the manager with voids...

    and the code above has nothing to do with my networkreply..
    what should i put instead of qsm?


  • Moderators

    I wrote that code quite a while ago ;) IIRC, the scheme was: create a replyFinished() slot like you do (+ store the reply, + set a notification bool), connect QNAM to it, then invoke the forever loop. This neatly changes an asynchronous API into a synchronous one (in QWS, I provide both for convenience).


Log in to reply
 

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