【发布时间】:2012-01-17 14:43:18
【问题描述】:
我在这方面遇到了一些困难,但基本上将页面加载到 QWebView 中是否可以获取在渲染页面过程中加载的内容的事件?
我正在使用 PySide,所以假设你已经有一个 QWebView 作为 'w'
w.load('http://www.unicorns-are-awesome.com/index.html')
那个 index.html 的内容看起来像:
<html>
...
<head>
<script src="something.js">
</head>
<body>
<img src="unicorns.jpg">
</body>
</html>
QWebView 必须同时下载 something.js 和 unicorns.jpg - 但到目前为止似乎还没有任何明显的方法可以为这些从属获取 downloadRequest 事件下载。
w.page() 发出“downloadRequest”的唯一时间是当您更改 QtWebView 中的 URL 时,即您只能获取“位置”栏中的内容的更新。
如何在您的 QtWebView 中收到网页下载的每个元素的通知?
更新:NetworkAccessManager 实施:
from MainWindow import MainWindow
from PySide.QtGui import QApplication
from PySide.QtCore import QCoreApplication
from PySide.QtWebKit import QWebView, QWebSettings
from PySide.QtNetwork import QNetworkReply
class TransferMonitor(object):
def __init__(self):
a = MainWindow._instance # "singleton"
b = a.findChild(QWebView, "browser")
nm = b.page().networkAccessManager()
nm.finished[QNetworkReply].connect( self.dump_url )
def dump_url(self, reply):
# This is probably unnecessary, but
# I wanted to be 100% sure that every get
# was 'fresh'.
QWebSettings.clearMemoryCaches()
# For now all we really do is just dump URLs
# as they're processed. Perhaps later we will intercept.
print reply.url().toString()
【问题讨论】:
-
你到底想做什么?实现进度条?
-
@Mhh Lecker - 更新了问题以反映目标。
标签: python qt qtwebkit pyside qt4.7