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. [Solved] Qt application sometimes is unable to connect to Internet?
Forum Updated to NodeBB v4.3 + New Features

[Solved] Qt application sometimes is unable to connect to Internet?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.6k 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.
  • CAD_codingC Offline
    CAD_codingC Offline
    CAD_coding
    wrote on last edited by
    #1

    Hi,
    In my application I have provided functionality to check for updates. This is done by reading an html file hosted on Google servers. This code works on some hardware while on some it just does not work. Probably because of Firewall or bad Anti-virus. But I am not sure what is the reason. So I wanted to know whether Qt provides any functionality to check why connection to the Internet is not happening? Probably an error message would do. Here is my code. I found it on one of the Wiki's but cannot remember the link.

    @//Header File
    #ifndef CHECKUPDATES_H
    #define CHECKUPDATES_H

    #include <QObject>
    #include <QNetworkAccessManager>
    #include <QNetworkRequest>
    #include <QNetworkReply>
    #include <QTimer>

    class CheckUpdates : public QObject
    {
    Q_OBJECT
    public:
    explicit CheckUpdates(bool *finish, QString *link);

    void checkUpdates();
    

    signals:

    public slots:

    private slots:
    void fileDownloaded(QNetworkReply *reply);
    void killChecking();

    private:
    QNetworkAccessManager manager;
    QTimer timer;
    bool *finished;
    QString *url;
    };

    #endif // CHECKUPDATES_H

    //Source File
    #include "checkupdates.h"
    #include <QDebug>

    CheckUpdates::CheckUpdates(bool *finish, QString *link) : QObject()
    {
    finished = finish;
    url = link;
    }

    void CheckUpdates::fileDownloaded(QNetworkReply *reply)
    {
    QString line = reply->readLine().simplified();
    if(line.contains("Secret Keyword"))
    {
    //success!
    }
    else
    {
    qWarning("Failure checking Updates");
    qDebug() << line << endl;
    qDebug() << reply->readAll().simplified();
    *url = "fail";
    }

    reply->deleteLater();
    *finished = true;
    

    }

    void CheckUpdates::killChecking()
    {
    disconnect(&manager, 0, 0, 0);
    disconnect(&timer, 0, 0, 0);
    *finished = true;
    }

    void CheckUpdates::checkUpdates()
    {
    connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(fileDownloaded(QNetworkReply*)));
    connect(&timer, SIGNAL(timeout()), this, SLOT(killChecking()));
    QNetworkRequest request(QUrl("https://googledrive.com/host/funky_code_by_Google/myFile.html"));
    manager.get(request);
    timer.start(10000);
    }@

    On some PC's this is working just fine, while on some the request gets killed by the timer. The strange thing is that the qWarning() is not getting thrown! Why is this strange stuff happening?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      michal.gjk
      wrote on last edited by
      #2

      To check an error message use reply->error().

      qWarning is not called because fileDownloaded never gets called most likely.

      1 Reply Last reply
      0
      • CAD_codingC Offline
        CAD_codingC Offline
        CAD_coding
        wrote on last edited by
        #3

        Thanks for your reply. I will check error() and it in my code.

        But can you please tell me why fileDownloaded() does not get called?
        It cannot be that connection is not happening because it would throw a qWarning() internally. But since there is no such warning the connection must be happening.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          michal.gjk
          wrote on last edited by
          #4

          When the timer runs out you're disconnecting from QNetworkAccessManager::finished() signal which would call fileDownloaded should the connection actually finish.

          1 Reply Last reply
          0
          • CAD_codingC Offline
            CAD_codingC Offline
            CAD_coding
            wrote on last edited by
            #5

            so should I refrain from using such a timer?
            But if I remove the timer, is it guaranteed that fileDownloaded() would be called eventually.
            Also if this is called after a fixed time interval then is it possible to set this time interval?
            So that I would no longer be needing the timer then.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              michal.gjk
              wrote on last edited by
              #6

              I don't think it's possible to set the timeout in the manager I think you'd be waitnig forever for the connection to timeout.

              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