Overriding page responses in QWebView

I am trying to intercept a page / form request in Qt QWebView and in some cases respond with alternative content.

QNetworkReply* ngcBrowser::createRequest(Operation operation, const QNetworkRequest& request, QIODevice* ioDevice) { view->page()->setNetworkAccessManager(this); QNetworkReply* response = NULL; if (request.url().path().endsWith("ajax")) { response = QNetworkAccessManager::createRequest(operation, request, ioDevice); response->write("{ success: true }"); } else { response = QNetworkAccessManager::createRequest(operation, request, ioDevice); } return response; } 

As you can see above, I overridden the QNAM createRequest method to receive all page requests and response using a JSON object if Url ends with the .ajax extension. However, it is difficult for me to write the data back to the network stream.

Any hints or tips on how to do this?

Cheers, Ben

Update:

Hi Abhijit, I tried to solve the problem, but could not connect the signal to the slot.

 QNetworkAccessManager* nam = view->page()->networkAccessManager(); bool status = QObject::connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyReceived(QNetworkReply*))); if(!status) { QErrorMessage errorMessage; errorMessage.showMessage("connect failed"); errorMessage.exec(); } 

Error:

Object :: connect: there is no such slot ngcBrowser :: replyRecieved (QNetworkReply *)

Update

Well, I managed to get it working, however, when I try to write to IODevice, this does not indicate its ReadOnly device.

Thanks for the help.

+4
source share
1 answer

There are many ways to do this. This is one way.

 connect(networkAccessManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyReceived(QNetworkReply*))) .... void replyReceived(QNetworkReply* reply) // reply slot { if(reply->request().url().path().endsWith("ajax")) { QByteArray array = reply->readll();/*reply is cleared after this call and will not contains anything.*/ /*Write the JSON wherever you want to in the array*/ reply->write(array); } } 

You need to configure this setting depending on which signal you want to listen to - answer QNAM or end with QNetworkReply, etc.

+1
source

All Articles