QWebView中的覆盖页面回复 [英] Overriding page replies in QWebView
问题描述
我试图在Qt的QWebView中拦截页面/表单请求,并在某些情况下以其他内容作为响应.
I'm attempting to intercept a page/form request within Qt's QWebView and respond in some cases 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;
}
正如您在上面看到的,我已经重写了QNAM createRequest方法,以接收所有页面请求,如果Url以.ajax扩展名结尾,则使用JSON对象进行响应.但是我发现很难将数据写回到网络流中.
As you can see above I've overridden the QNAM createRequest method to receive all page requests and respond with a JSON object if the Url ends with a .ajax extension. However i'm finding it hard to write data back into the network stream.
有关如何进行此操作的任何提示或提示?
Any hints or tips on how to go about this?
干杯, 本
更新:
阿比吉斯, 我已经尝试过您的解决方案,但是它无法将信号连接到插槽.
Hi Abhijith, I've attempted your solution however it fails to 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();
}
错误:
Object :: connect:没有此类插槽ngcBrowser :: replyRecieved(QNetworkReply *)
Object::connect: No such slot ngcBrowser::replyRecieved(QNetworkReply*)
更新:
好了,但是我设法写IODevice时失败了,表明它是ReadOnly设备.
Ok I've managed to get his working however when i attempt to write to the IODevice is fails indicating its a ReadOnly device.
感谢所有帮助.
推荐答案
有很多方法可以做到这一点.这是一种方式.
There are many ways of doing 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);
}
}
您必须根据要收听的信号进行微调-从QNAM完成答复,或从QNetworkReply完成答复.
You Have to fine tune this depending on which signal you want to listen to - replyfinished from QNAM or finished from QNetworkReply etc.
这篇关于QWebView中的覆盖页面回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!