Reputation: 33
I am trying to read the response data from QWebView using the QNetworkAccessManager.
I followed the instrunction in the reply found here: how to get response in QtWebKit
I subclassed the QNetworkAccessManager, then set the QWebView to use the my class:
ui->explorer->page()->setNetworkAccessManager(new myNetworkAccessManager());
Then I override the createRequest function and try to read the data:
#include "mynetworkaccessmanager.h"
myNetworkAccessManager::myNetworkAccessManager(QObject *parent) :
QNetworkAccessManager(parent)
{
}
QNetworkReply *myNetworkAccessManager::createRequest ( Operation op, const QNetworkRequest & req, QIODevice * outgoingData){
QNetworkReply *reply = QNetworkAccessManager::createRequest(op, req, outgoingData);
qDebug() << reply->readAll();
return reply;
}
I still get empty data. What am i doing wrong ?
Upvotes: 1
Views: 2520
Reputation: 512
QNetworkAccessManager, and QNetworkReply, are all Asynchronous IO classes, A.K.A non-blocking IO, which means createRequest() will return immediately without waiting for QNetworkReply to finish fetching/creating request. So if you read it immediately, almost definite chance it will be empty.
What you need to do is connect QNetworkReply's readyread() signal, which will be emitted after your data is ready. For more details read the docs
Edit: Oh and one more thing, as the question you linked answered, do use peek() instead of readAll() since your QWebView will not receive the data after you read it (QIODevices purge all system resources after one read)
Upvotes: 2