使用QtWebKit时如何知道网页何时加载? [英] How to know when a web page is loaded when using QtWebKit?

查看:36
本文介绍了使用QtWebKit时如何知道网页何时加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

QWebFrameQWebPage 都有 void loadFinished(bool ok) 信号,可用于检测网页何时完全加载.问题是当网页有一些异步加载的内容 (ajax) 时.在这种情况下如何知道页面何时完全加载?

Both QWebFrame and QWebPage have void loadFinished(bool ok) signal which can be used to detect when a web page is completely loaded. The problem is when a web page has some content loaded asynchronously (ajax). How to know when the page is completely loaded in this case?

推荐答案

我实际上并没有这样做,但我认为您可能能够使用 QNetworkAccessManager 实现您的解决方案代码>.

I haven't actually done this, but I think you may be able to achieve your solution using QNetworkAccessManager.

您可以使用 networkAccessManager() 函数从 QWebPage 获取 QNetworkAccessManager.QNetworkAccessManager 有一个信号 finished ( QNetworkReply * reply ) 每当文件被触发时都会触发由 QWebPage 实例请求.

You can get the QNetworkAccessManager from your QWebPage using the networkAccessManager() function. QNetworkAccessManager has a signal finished ( QNetworkReply * reply ) which is fired whenever a file is requested by the QWebPage instance.

finished 信号为您提供了一个 QNetworkReply 实例,您可以从中获取原始请求的副本,以便识别请求.

The finished signal gives you a QNetworkReply instance, from which you can get a copy of the original request made, in order to identify the request.

因此,创建一个插槽以附加到 finished 信号,使用传入的 QNetworkReply 的方法来确定哪个文件刚刚完成下载,如果它是您的 Ajax 请求,请执行您需要的任何处理要做.

So, create a slot to attach to the finished signal, use the passed-in QNetworkReply's methods to figure out which file has just finished downloading and if it's your Ajax request, do whatever processing you need to do.

我唯一需要注意的是,我以前从未这样做过,所以我不能 100% 确定它会起作用.

My only caveat is that I've never done this before, so I'm not 100% sure that it would work.

另一种选择可能是使用 QWebFrame 的方法将对象插入到页面的对象模型中,并插入一些 JavaScript,然后在 Ajax 请求完成时通知您的对象.这是一种稍微有点hacker的方法,但绝对可行.

Another alternative might be to use QWebFrame's methods to insert objects into the page's object model and also insert some JavaScript which then notifies your object when the Ajax request is complete. This is a slightly hackier way of doing it, but should definitely work.

第二个选项对我来说似乎更好.工作流程如下:

The second option seems better to me. The workflow is as follows:

为 QWebFrame::javascriptWindowObjectCleared() 信号附加一个插槽.此时调用QWebFrame::evaluateJavascript()添加类似如下代码:window.onload = function() {//页面已完全加载}

Attach a slot to the QWebFrame::javascriptWindowObjectCleared() signal. At this point, call QWebFrame::evaluateJavascript() to add code similar to the following: window.onload = function() { // page has fully loaded }

在该函数中放入您需要的任何代码.您可能希望通过 QWebFrame::addToJavaScriptWindowObject() 将 QObject 添加到页面,然后在该对象上调用函数.此代码仅在页面完全加载时才会执行.

Put whatever code you need in that function. You might want to add a QObject to the page via QWebFrame::addToJavaScriptWindowObject() and then call a function on that object. This code will only execute when the page is fully loaded.

希望这能回答问题!

这篇关于使用QtWebKit时如何知道网页何时加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆