5

The function CheckSite() is called with an url like http://example.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));
}
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Emilio
  • 3,901
  • 11
  • 44
  • 50

2 Answers2

1

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
  • 45,767
  • 19
  • 102
  • 147
  • Should I include moc file in my main() source file? – Emilio May 06 '10 at 13:02
  • 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 May 06 '10 at 19:18
0

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

guruz
  • 1,604
  • 14
  • 21
  • Actually not. Tonight i try including QCoreApplication and app.exec() in my code (is a console application). – Emilio May 06 '10 at 13:04
  • 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 May 06 '10 at 17:53