0

After debugging, I'm sure that the replyFinish() slot is not called when I call this->getNetReply(). These are my files, in the main() fumction I call the getNetReply this way: Networking a; a.getNetReply(); I did add QT+=network to my qmake. Please help me. Thank you very much.

my networking.cpp file

#include "networking.h"
#include <QUrl>
#include <QNetworkRequest>

// constructor    

void Networking::getNetReply(){
    QNetworkAccessManager *man  = new QNetworkAccessManager(this);
    QObject::connect(man, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
    qDebug() << "connected";
    QNetworkRequest req;
    req.setUrl(QUrl("http://www.google.com"));
    man->get(req);

}
// this method not called
void Networking::replyFinished(QNetworkReply *reply){
    QByteArray data = reply->readAll();
    QString str = QString(data);
    this->netRep = str;
    code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
}

my networking.h header file:

#ifndef NETWORKING_H
#define NETWORKING_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>

class Networking : public QObject
{
    Q_OBJECT
public:
    QString netRep;
    int code;
    explicit Networking(QObject *parent = 0);
    void getNetReply();

public slots:
    void replyFinished(QNetworkReply*);

};

#endif // NETWORKING_H
Huy Ngo
  • 194
  • 10
  • Can you show the whole `main()` function too? – vahancho Sep 16 '14 at 11:37
  • 4
    Maybe you should check if `connect` method returns ok or failure. On the other hand, you can try to clean and rebuild the moc of the class. remarks: `Networkig` is `QObject` because of this you don't need to write `QObject::` on `connect` method. All classes that inherits from `QObject`has a member called `connect`. – eferion Sep 16 '14 at 11:41
  • Your code is good and it should work. Make sure that firewalls or antivirus don't block your app. – Jablonski Sep 16 '14 at 13:09
  • This is a strange problem. I did do this downloader more than one thousand times and this is the first time I run into that problem. I think I should delete my project and stop working. – Huy Ngo Sep 16 '14 at 13:20
  • 1
    You could connect to the `error(QNetworkReply::NetworkError)` signal of the `QNetworkReply` and check if that gives an indication of what's going wrong. I don't think think the `finished()` signal is emitted in the error case. – lazycoder Sep 16 '14 at 21:23

1 Answers1

0

The get() function returns a QNetworkReply object. Connect to the error signal of that object and it will tell you if an error happens (and which).

Silicomancer
  • 8,604
  • 10
  • 63
  • 130