QNetworkAccessManager signals not working
-
I don't know why but it doesn't work at all, not the finished, error, or sslErrors, none of them:
auto manager = new QNetworkAccessManager; QNetworkRequest request; request.setUrl(QUrl("http://qt-project.org")); request.setRawHeader("User-Agent", "MyOwnBrowser 1.0"); QNetworkReply *reply = manager->get(request); connect(reply, SIGNAL(readyRead()), this, SLOT(replyReadyRead())); connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(replyError(QNetworkReply::NetworkError))); connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(replySslErrors(QList<QSslError>)));I even tried to use the new syntax like
connect(networkReply, static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), [=](QNetworkReply::NetworkError code){ /* ... */ });but doesn't work either...
What is happening? -
I don't know why but it doesn't work at all, not the finished, error, or sslErrors, none of them:
auto manager = new QNetworkAccessManager; QNetworkRequest request; request.setUrl(QUrl("http://qt-project.org")); request.setRawHeader("User-Agent", "MyOwnBrowser 1.0"); QNetworkReply *reply = manager->get(request); connect(reply, SIGNAL(readyRead()), this, SLOT(replyReadyRead())); connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(replyError(QNetworkReply::NetworkError))); connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(replySslErrors(QList<QSslError>)));I even tried to use the new syntax like
connect(networkReply, static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), [=](QNetworkReply::NetworkError code){ /* ... */ });but doesn't work either...
What is happening?- check the return value of connect()
- check the console for warnings regarding the connect() statements
- show some more code, what happen with the reply/QNetworkAccessManager?
-
1 - All of them is returning true
2 - There's nothing on the console
3 - I just realized that I forgot to connectfinished...auto manager = new QNetworkAccessManager; QNetworkRequest request; request.setUrl(QUrl("http://qt-project.org")); request.setRawHeader("User-Agent", "MyOwnBrowser 1.0"); QNetworkReply *reply = manager->get(request); auto r1 = connect(reply, SIGNAL(finished()), this, SLOT(replyFinished())); auto r2 = connect(reply, SIGNAL(readyRead()), this, SLOT(replyReadyRead())); auto r3 = connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(replyError(QNetworkReply::NetworkError))); auto r4 = connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(replySslErrors(QList<QSslError>))); qDebug() << r1 << r2 << r3 << r4;The rest of the code is the slots, but it has nothing special:
void NYTimes::replyFinished() { qDebug() << "it is working"; } void NYTimes::replyReadyRead() { qDebug() << "ready to read"; } void NYTimes::replyError(QNetworkReply::NetworkError error) { qDebug() << error; } void NYTimes::replySslErrors(QList<QSslError> errors) { qDebug() << errors; }And yes, it's not working either.
-
1 - All of them is returning true
2 - There's nothing on the console
3 - I just realized that I forgot to connectfinished...auto manager = new QNetworkAccessManager; QNetworkRequest request; request.setUrl(QUrl("http://qt-project.org")); request.setRawHeader("User-Agent", "MyOwnBrowser 1.0"); QNetworkReply *reply = manager->get(request); auto r1 = connect(reply, SIGNAL(finished()), this, SLOT(replyFinished())); auto r2 = connect(reply, SIGNAL(readyRead()), this, SLOT(replyReadyRead())); auto r3 = connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(replyError(QNetworkReply::NetworkError))); auto r4 = connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(replySslErrors(QList<QSslError>))); qDebug() << r1 << r2 << r3 << r4;The rest of the code is the slots, but it has nothing special:
void NYTimes::replyFinished() { qDebug() << "it is working"; } void NYTimes::replyReadyRead() { qDebug() << "ready to read"; } void NYTimes::replyError(QNetworkReply::NetworkError error) { qDebug() << error; } void NYTimes::replySslErrors(QList<QSslError> errors) { qDebug() << errors; }And yes, it's not working either.
@Defohin
if all connects are successfull (by returning true), the only thing i can think of is that the reply (or QNAM) or the object holding the slots are deleted? -
Hi,
can you try with a site that does not redirect to https e.g. http://en.cppreference.com/w/. Maybe that's a problem.
-Michael. -
The problem was on the instance of the class, I had to use it as a pointer.
auto nytimes = new NYTimes; nytimes->parse();instead of:
NYTimes nytimes; nytimes.parse();Is there a reason for that?
Hi
It depends on the contextvoid Func() {
auto nytimes = new NYTimes;
nytimes->parse();
} // after here NYTimes still livesvoid Func() {
NYTimes nytimes;
nytimes.parse();
} // after here NYTimes is no more -
@Defohin said in QNetworkAccessManager signals not working:
NYTimes nytimes; on the constructor of the QMainWindow
So it means that at the time when
MainWindow was shown, nytimes would have been deleted.
Its called "running out of scope". Applies to all variables on the stack.