覆盖 QWebView 中的页面回复

发布于 2024-11-08 15:07:57 字数 1428 浏览 0 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巨坚强 2024-11-15 15:07:57

有很多方法可以做到这一点。这是一种方法。

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 完成的回复等。

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);

    }
}

You Have to fine tune this depending on which signal you want to listen to - replyfinished from QNAM or finished from QNetworkReply etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文