QNetworkAccessManager never opens connection, QNetworkReply never gets any data.



  • Hello,

    I am fairly new to C++ and Qt and am having some issues with QNetworkAccessManager using Qt 5.2. I do have an event loop running just fine so I know that's not the issue. If anyone could point me in the right direction that would be appreciated.. this code is kind of rough as I have just been trying to get it to do anything before properly implementing it.

    Header file:
    @
    #include <QtCore/QObject>
    #include "QtNetwork/QNetworkReply"

    class QNetworkAccessManager;
    class QTimer;

    class DownloadTest : public QObject
    {
    Q_OBJECT;

    public:

    /**

    • Standard constructor for DownloadTest.
      */
      DownloadTest(QObject *parent=NULL);

    /**

    • Standard deconstructor.
      */
      ~DownloadTest();

    /**

    • Begins the operation to get the file at the url.
    • @param[in] url
    • The URL to the file to get.
      */
      bool GetFile(const QUrl url);

    public slots:

    void replyFinished(QNetworkReply* reply);

    void finished();

    void readyRead();

    void netError(QNetworkReply::NetworkError error);

    void loopDone();

    private:

    /**

    • Pointer to the access manager we are using.
      */
      QNetworkAccessManager * _AccessManager;

    /**

    */
    QNetworkReply * _Reply;

    QTimer * _loopTest;
    };
    @

    CPP file:
    @
    #include "DownloadTest.h"
    #include "QtNetwork/QNetworkAccessManager"
    #include "QtNetwork/QNetworkRequest"
    #include "QtNetwork/QNetworkReply"
    #include "QtCore/QUrl"
    #include "QtCore/QTimer"

    DownloadTest::DownloadTest(QObject parent/NULL/):
    QObject(parent)
    {
    _AccessManager = new QNetworkAccessManager(this);
    _loopTest = new QTimer(this);
    _loopTest->setInterval(0);
    _Reply = NULL;
    connect(_AccessManager, SIGNAL(finished(QNetworkReply
    )), this, SLOT(replyFinished(QNetworkReply*)));
    connect(_loopTest, SIGNAL(timeout()), SLOT(loopDone()));
    _loopTest->start();
    }

    DownloadTest::~DownloadTest()
    {
    qDebug() << "Deleting updater\n";
    }

    void DownloadTest::replyFinished(QNetworkReply* reply)
    {
    qDebug() << "Reply finished\n";
    }

    bool DownloadTest::GetFile(const QUrl url)
    {
    qDebug() << "Starting request\n";
    QNetworkRequest request;
    request.setUrl(url);
    _Reply = _AccessManager->get(request);
    connect(_Reply, SIGNAL(readyRead()), this, SLOT(readyRead()));
    connect(_Reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(netError(QNetworkReply::NetworkError)));
    connect(_Reply, SIGNAL(finished()), SLOT(finished()));
    return _Reply->isRunning();
    }

    void DownloadTest::readyRead()
    {
    qDebug() << "Got data\n";
    }

    void DownloadTest::netError(QNetworkReply::NetworkError error)
    {
    qDebug() << "Error: " << error << "\n";
    }

    void DownloadTest::finished()
    {
    qDebug() << "Finished.\n";
    }

    void DownloadTest::loopDone()
    {
    if (_Reply != NULL)
    {
    qDebug() << "We are : "<< _Reply->isRunning() << " with:" << _Reply->bytesAvailable() << " bytes\n";
    }
    }
    @

    This is part of an existing application using Qt and this is the first issue I have ran into that has really stumped me.

    None of the signals ever get emitted, besides the one from the timer for the loopDone function. It will run forever more always with 0 bytes available, I called the error() function on the QNetworkReply but that always returns QNetworkReply::NoError also.

    I checked TCPView and it never tries to establish a connection.


Log in to reply
 

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