Re: PyQT: QWebView with custom QNetworkAccessManager

Discussion in 'Python' started by Gelonida, Feb 2, 2011.

  1. Gelonida

    Gelonida Guest

    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
    NetworkAccessManager

    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):
    QNetworkAccessManager.__init__(self)
    self.setCache(old_manager.cache())
    self.setCookieJar(old_manager.cookieJar())
    self.setProxy(old_manager.proxy())
    self.setProxyFactory(old_manager.proxyFactory())

    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 = web.page().networkAccessManager()
    new_manager = MyNetworkAccessManager(old_manager)
    web.page().setNetworkAccessManager(new_manager)

    app = QtGui.QApplication(sys.argv)
    web = QtWebKit.QWebView()
    set_new_manager()
    web.setUrl( QtCore.QUrl("http://www.google.com") )
    web.show()

    sys.exit(app.exec_())
     
    Gelonida, Feb 2, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sgperone
    Replies:
    1
    Views:
    1,106
    sgperone
    Jun 28, 2009
  2. Gelonida
    Replies:
    1
    Views:
    1,603
    David Boddie
    Feb 2, 2011
  3. Gelonida
    Replies:
    5
    Views:
    702
    Gelonida
    Feb 24, 2011
  4. Gelonida
    Replies:
    0
    Views:
    592
    Gelonida
    Feb 19, 2011
  5. Gelonida
    Replies:
    0
    Views:
    265
    Gelonida
    Mar 3, 2011
Loading...

Share This Page