1

I am writing a simple qt network application. I try to read the data from the QNetworkReply but it seems that the finished signal never emit. What happens?

QByteArray utils::Login(QString account)
{
    QNetworkAccessManager* manager = new QNetworkAccessManager();
    QNetworkRequest* request = new QNetworkRequest();
    request->setUrl(QUrl(Urls::loginUrl));

    request->setRawHeader("Host", "10.136.2.5");
    request->setRawHeader("Referer", "http://10.136.2.5/jnuweb/");
    request->setRawHeader("Content-Type", "application/json; charset=utf-8");
    request->setRawHeader("Connection", "keep-alive");
    request->setRawHeader("X-Requested-With", "XMLHttpRequest");
    request->setRawHeader("Accept", "*/*");
    request->setRawHeader("Accept-Encoding", "deflate");

    QJsonObject* requestContent = new QJsonObject();
    requestContent->insert("user", QJsonValue(account));
    requestContent->insert("password", QJsonValue(Urls::initPassword));

    QNetworkReply* reply = manager-> post(*request, QJsonDocument(*requestContent).toJson(QJsonDocument::Compact));

    QObject::connect(reply, &QNetworkReply::finished, [=]()
    {
        QList<QPair<QByteArray, QByteArray>> responses = reply -> rawHeaderPairs();
        qDebug() << responses;
    });
}
Ilya
  • 4,583
  • 4
  • 26
  • 51
Aaron
  • 31
  • 2
  • Yes, what happens after the function is called? Is there any event loop in that thread? – Alexander V Dec 28 '17 at 05:47
  • @AlexanderVX Thanks for reply, I started debugging it but the program just paused in connect function. No output from QDebug. There is no such an even loop I think. – Aaron Dec 28 '17 at 05:57
  • Paused on itself? Cannot pass `connect`? Or it passes and terminates? It must have an event loop for posting the network data: either in that thread or GUI thread always has event loop but it is hidden in QApplication. Do you have at least `app.exec()` for the running? I suspect you use main thread. – Alexander V Dec 28 '17 at 05:59
  • If do not use lambda signal & slot everything remains same? – Tazo leladze Dec 28 '17 at 06:15
  • Also I wonder if you have access to the server and if you can tell us this request goes to server? – Tazo leladze Dec 28 '17 at 06:16
  • It seems more logical to connect prior to posting the request. – user2672165 Dec 28 '17 at 06:46
  • 2
    It is possible that `finished` is emitted before your `connect` statement. Try connecting the [`finished` signal of `QNetworkAccessManager`](http://doc.qt.io/qt-5/qnetworkaccessmanager.html#finished) and see what happens. – thuga Dec 28 '17 at 08:39
  • Well, after more investigation, I changed the return type to void and successfully solve this problem. But I just do not know why. – Aaron Dec 28 '17 at 14:44
  • @thuga It depends, because Qt events loop won't execute slots if the function is not terminated. See [here](https://stackoverflow.com/questions/16627573/qt-signal-slot-connection-qnetworkaccessmanager) and [there](https://stackoverflow.com/questions/29370840/qt-connect-a-signal-after-a-request-is-sent-in-qnetworkaccessmanager). – Delgan Sep 11 '18 at 09:04

0 Answers0