Question

I'm having some difficulty with this, but basically load a page into a QWebView is it possible to get events on the content that gets loaded in the process of rendering the page?

I'm using PySide, so assuming you already have a QWebView as 'w'

w.load('http://www.unicorns-are-awesome.com/index.html')

And the contents of that index.html look like:

<html>
    ...
    <head> 
         <script src="something.js">
    </head>
    <body>
         <img src="unicorns.jpg">
    </body>
</html>

The QWebView has to download both something.js and unicorns.jpg - but there so far does not appear to be any obvious way to get downloadRequest events for these subordinate downloads.

The only time that 'downloadRequest' is emitted by w.page() is when you change the URL in the QtWebView, i.e. you only get updates for what would be in the 'Location' bar.

How can you get notified for every element that a webpage downloads in your QtWebView?

Update: NetworkAccessManager Implementation:

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()
Was it helpful?

Solution

You would need to implement a QNetworkAccessManager, override createRequest(), and call QWebPage::setNetworkAccessManager(). I'm not sure if this is possible in PySide.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top