Emilio
Emilio

Reputation: 4031

Qt QNetworkAccessManager does not emit signals

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));
}

Upvotes: 5

Views: 2016

Answers (2)

guruz
guruz

Reputation: 1614

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

Upvotes: 0

Kaleb Pederson
Kaleb Pederson

Reputation: 46469

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?

Upvotes: 1

Related Questions