Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: PyQT: QWebView with custom QNetworkAccessManager (http://www.velocityreviews.com/forums/t742964-re-pyqt-qwebview-with-custom-qnetworkaccessmanager.html)

Gelonida 02-02-2011 01:37 AM

Re: PyQT: QWebView with custom QNetworkAccessManager
 
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_())







All times are GMT. The time now is 11:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57