Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Check internet connection with QNetworkAccessManager question.

Check internet connection with QNetworkAccessManager question.

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 6.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #1

    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)...

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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().

      (Z(:^

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Leon
        wrote on last edited by
        #3

        [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?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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).

          (Z(:^

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved