I am using a QWebview to display html generated by the same program. Now the html can have references to other resources, e.g. <FRAME src=...>
in a fram set. When the browser would start downloading that resource, I must intercept that request and suply the content myself, since there's no webserver involved. Where are the hooks that I may use to catch up the requested url and supply the generated content?
to create the browser widget:
self.browser = QWebView()
self.layout.addWidget(self.browser)
to load the frame set:
self.browser.setHtml(ret.text)
Now what I would expect to find is some signal and then
self.browser.requestURI.connect(myhandler)
But I don't see anything alike it. What is the better approach here?
EDIT:
The major problem seems to be using setHtml. Thus, all loading mechanisms appear to be bypassed. With load() in combination with a QNetworkAccessManager I had better results (see below). Now the response-object is offered to my content manager, however, I sofar failed to write anything to the response object (or to instantiate a fresh one). It can be opened passing an access mode parameter. Then the READ-ONLY error disappears, but still write returns -1.
I rephrase the title of this question accordingly.
from PyQt5.Qt import * # @UnusedWildImport
class ContentManager(object):
def handleRequest(self, request, response):
# response.writeData(bytearray("hello new year", "utf-8")) #THIS WORKS NOT
return response
class NAM(QNetworkAccessManager):
def __init__(self, contentManager):
super().__init__()
self.contentManager = contentManager
def createRequest(self, operation, request, device):
response = super().createRequest(operation, request, device)
return self.contentManager.handleRequest(request, response)
class Browser(QWidget):
def __init__(self):
super().__init__()
def open(self, url):
self.browser.load(QUrl(url))
def build(self, contentManager):
layout = QVBoxLayout(self)
view = QWebView()
page = view.page(); view.setPage(page)
page.setNetworkAccessManager(NAM(contentManager))
layout.addWidget(view)
self.browser = view
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Browser()
w.build(ContentManager())
w.open("index.html")
w.show()
app.exec_()