覆盖 QWebView 中的页面回复
我试图在 Qt 的 QWebView 中拦截页面/表单请求,并在某些情况下使用替代内容进行响应。
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 对象进行响应。但是我发现很难将数据写回到网络流中。
关于如何解决这个问题有任何提示或技巧吗?
干杯, Ben
更新:
嗨,Abhijith, 我已经尝试过您的解决方案,但它无法将信号连接到插槽。
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*)
更新:
好的,我已经设法让他工作了,但是当我尝试写入时IODevice 失败,表明它是只读设备。
感谢您的所有帮助。
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;
}
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?
Cheers,
Ben
Update:
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();
}
Error:
Object::connect: No such slot ngcBrowser::replyRecieved(QNetworkReply*)
Update:
Ok I've managed to get his working however when i attempt to write to the IODevice is fails indicating its a ReadOnly device.
Thanks for all the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有很多方法可以做到这一点。这是一种方法。
您必须根据您想要收听的信号进行微调 - 从 QNAM 完成的回复或从 QNetworkReply 完成的回复等。
There are many ways of doing this . This is one way.
You Have to fine tune this depending on which signal you want to listen to - replyfinished from QNAM or finished from QNetworkReply etc.