PyQT: QWebView with custom QNetworkAccessManager

Gelonida gelonida at
Wed Feb 2 02:37:06 CET 2011

On 02/02/2011 12:31 AM, Gelonida wrote:
> Hi,
> I would like to subclass QNetworkAccessManager and
> create a subclass of QWebView, that will use the subclassed
> QNetworkAccessManager for all accesses.
> Is this possible?
> I have really no idea when and how I could achieve this.
> Thanks in advance for any suggestions / pointers

Well I answer my own question.

In fact my first experiments failed horribly due to a tiny PyQt detail.

I expected that, the variable new_manager does not have to be persistent.

I naively assumed, that a call to setNetworkAccessManager() would keep a
reference to new_manager and thus avoid its destruction this does not
seem to be the case.

Below an example of how to create a QQWebview with a custom

import sys
import PyQt4.QtGui as QtGui
import PyQt4.QtCore as QtCore
import PyQt4.QtWebKit as QtWebKit
from PyQt4.QtNetwork import QNetworkAccessManager

class MyNetworkAccessManager(QNetworkAccessManager):
    def __init__(self, old_manager):

    def createRequest(self, operation, request, data):
        print "mymanager handles ", request.url()
        return QNetworkAccessManager.createRequest(
                self, operation, request, data)

def set_new_manager(web):
    global new_manager # if this line is commented I will se
    old_manager =
    new_manager = MyNetworkAccessManager(old_manager)

app = QtGui.QApplication(sys.argv)
web = QtWebKit.QWebView()
web.setUrl( QtCore.QUrl("") )


More information about the Python-list mailing list