QNetworkAccessManager signals not working
-
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? -
It's not being deleted, I'm using pointers to test if it has something to do with that, but it doesn't work either.
-
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. -
It doesn't work either, I really don't know what is going on.
-
This post is deleted! -
Hi,
please try to makeQNetworkReply *reply
a class member variable. As you have it now it gets lost as soon as you leave the function. You will need it anyway in thereadyRead
slot.
-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 -
I was calling
NYTimes nytimes;
on the constructor of theQMainWindow
-
@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. -
Thank you, it's solved