如何从 QWebPage 使用的 QNetworkReply 读取数据?

发布于 2024-10-08 08:33:43 字数 1453 浏览 7 评论 0原文

我使用 QWebPage 下载网页以及其所有资源。同时,我想在此过程中获取 Qt 下载的原始数据。通过读取 QNetworkReply 中的数据来执行此操作href="http://doc.qt.nokia.com/latest/qnetworkaccessmanager.html#finished" rel="nofollow">void QNetworkAccessManager::finished(QNetworkReply * 回复) signal 不是一个好的解决方案,因为数据可能已经被 QWebPage 本身读取。这是因为

QNetworkReply 是一个顺序访问 QIODevice,表示一旦数据 从对象中读取,它不再 由设备保存。

根据QNetworkReply详细描述

但是,QWebPage 可以配置为使用自定义QNetworkAccessManager与覆盖 createRequest 方法

QNetworkReply * QNetworkAccessManager::createRequest ( Operation op, const QNetworkRequest & req, QIODevice * outgoingData = 0 )

我认为正确的解决方案是为 QNetworkReply 创建代理并在 createRequest 中返回它方法。该代理应该允许从回复中读取数据,就像原始 QNetworkReply 的情况一样(以便 QWebPage 可以从中读取数据),但同时该代理应该允许用于在 QWebPage 读取数据后由其他对象读取数据。换句话说,我们需要用于 QNetworkReplytee IODevice 基类。

这个代理怎么写呢?

I use QWebPage to download a webpage as well as all its resources. At the same time I'd like to get hold on raw data being downloaded by Qt during this process. Doing this by reading data from QNetworkReply in void QNetworkAccessManager::finished(QNetworkReply * reply)
signal is not a good solution as data could have been already read by QWebPage itself. This is because

QNetworkReply is a sequential-access
QIODevice, which means that once data
is read from the object, it no longer
kept by the device.

according to detailed description of QNetworkReply.

However QWebPage can be configured to use custom QNetworkAccessManager with overriden createRequest method

QNetworkReply * QNetworkAccessManager::createRequest ( Operation op, const QNetworkRequest & req, QIODevice * outgoingData = 0 )

I think the right solution would be to create a proxy for QNetworkReply and return it in the createRequest method. This proxy should allow for reading data from reply as is the case with the original QNetworkReply (so that QWebPage could read data from it) but at the same time this proxy should allow for reading data by other objects after it have been read by QWebPage. In other words we need tee for QNetworkReply's IODevice base class.

How to write this proxy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

随风而去 2024-10-15 08:33:43

看起来有人已经想要同样的东西并编写了 代理对于 QNetworkReply

It looks like someone has already wanted the same and wrote a proxy for the QNetworkReply.

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