views:

259

answers:

2

The function CheckSite() is called with an url like http://site.com, it initializes a QNetworkAccessManager object and connect() slots and signals.

The manger->get() call seems work (it generates http traffic) but does not call the slot replyFinished() at the request end.

What's wrong with this code?

#include <QtCore>
#include <QtNetwork>

class ClientHandler : public QObject
{
Q_OBJECT
  QNetworkAccessManager *manager;
private slots:
  void replyFinished(QNetworkReply *);
public:
  void CheckSite(QString url);
};

void ClientHandler::replyFinished(QNetworkReply *reply) { qDebug() << "DONE"; }

void ClientHandler::CheckSite(QString url) {
  QUrl qrl(url);
  manager = new QNetworkAccessManager(this);
  connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
  manager->get(QNetworkRequest(qrl));
}
A: 

Nothing. I wrapped it so it was fully functional and it works fine:

// placed in client.cpp
#include <QtDebug>
#include <QCoreApplication>

/* YOUR CODE */

int main(int argc, char *argv[])
{
        QCoreApplication app(argc, argv);
        ClientHandler handler;
        handler.CheckSite("www.google.com");
        return app.exec();

}

#include "client.moc"

It output "DONE" as expected. Maybe the site you're checking really isn't returning? Maybe it needs authentication or is producing ssl errors?

Kaleb Pederson
Should I include moc file in my main() source file?
Emilio
The `#include "client.moc"` is only necessary when you don't have your class defined in a header file (and you're using `qmake` as your build tool). Since I had everything in client.cpp, I needed that so everything would be picked up and link correctly.
Kaleb Pederson
A: 

What code do you have around that? Do you spin an event loop somewhere? e.g. qapp.exec() ?

guruz
Actually not. Tonight i try including QCoreApplication and app.exec() in my code (is a console application).
Emilio
I've added QCoreApplication app(argc, argv); /* objects and method calls */ return app.exec(); in the top main() function, but it doesn't fix anything.
Emilio