如何在 QtWebKit 中获取响应
我是 QtWebKit 的初学者,我构建了加载页面的简单 Web 框架(服务器端) 当我从这个页面提交数据时,我喜欢在 C++ 端捕获来自服务器的响应字符串 我该怎么做?
im beginner with QtWebKit i build simple web frame that loaded page ( server side )
and when from this page i submit data i like to catch the response string from the server in the c++ side
how can i do that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 Qt(我是新手)进行了修改,找到了一种捕获 WebKit 下载的所有资源的方法。具体方法如下:
1) 创建您自己的 QNetworkAccessManager 子类
2) 在派生类中,重写虚函数 createRequest
3) 调用基类实现以获取响应对象。之后你可以看看
URL(或其他参数)并确定是否需要捕获该特定资源
4) 如果需要 - 将readyRead信号连接到某个将捕获数据的插槽
5) 在该插槽中调用peek函数来读取数据,以便WebKit也将获取数据
6) 创建 QWebPage 对象后,调用 setNetworkAccessManager 并传递步骤 1 中新创建的子类实例)
就这样 - 享受吧!
I tinkered around with Qt (which I'm new to) and found a way to catch all resources downloaded by WebKit. Here's how:
1) Create your own subclass of QNetworkAccessManager
2) In your derived class, override virtual function createRequest
3) Call base class implementation to get the response object. After that you can look at
the URL (or other parameters) and determine whether you need to capture that particular resource or not
4) if you do - connect readyRead signal to some slot that will capture the data
5) in that slot call peek function to read data so that WebKit will get the data also
6) After creating QWebPage object, call setNetworkAccessManager and pass a newly created instance of your subclass from step 1)
That's it - enjoy!
您可以使用
QNetworkReply
类。QWebPage
实例具有networkAccessManager()
方法返回QNetworkAccessManager
实例能够发送请求和接收响应。您需要查找其
finished
信号。void QNetworkAccessManager::finished (QNetworkReply * 回复)
QNetworkReply
又是QIODevice
因此您可以调用它的readAll()
方法以接收响应数据。您可能还会发现这个问题很有用。
You can use
QNetworkReply
class for it.QWebPage
instances havenetworkAccessManager()
method that returns aQNetworkAccessManager
instance capable of sending requests and receiving responses.You need to look for its
finished
signal.void QNetworkAccessManager::finished ( QNetworkReply * reply )
QNetworkReply
in its turn is an inheritor ofQIODevice
therefore you are able to call itsreadAll()
method in order to receive the response data.You may also find this question useful.