I'm working on a kiosk web browser using Qt and PyQt4. QWebView seems to work quite well except for one quirk.
If a URL fails to load for any reason, I want to redirect the user to a custom error page. I've done this using the loadFinished() signal to check the result, and change the URL to the custom page if necessary using QWebView.load(). However, any page I attempt to load here fails to pull in external resources like CSS or images.
Using QWebView.load() to set the initial page at startup seems to work fine, and clicking any link on the custom error page will result in the destination page loading fine. It's just the error page that doesn't work.
I'm really not sure where to go next. I've included the source for an app that will replicate the problem below. It takes a URL as a command line argument - a valid URL will display correctly, a bad URL (eg. DNS resolution fails) will redirect to Google, but with the logo missing.
import sys from PyQt4 import QtGui, QtCore, QtWebKit
class MyWebView(QtWebKit.QWebView):
def __init__(self, parent=None):
QtWebKit.QWebView.__init__(self, parent)
self.resize(800, 600)
self.load(Q开发者_运维技巧tCore.QUrl(sys.argv[1]))
self.connect(self, QtCore.SIGNAL('loadFinished(bool)'), self.checkLoadResult)
def checkLoadResult(self, result):
if (result == False):
self.load(QtCore.QUrl('http://google.com'))
app = QtGui.QApplication(sys.argv)
main = MyWebView()
main.show()
sys.exit(app.exec_())
If anyone could offer some advice it would be greatly appreciated.
Do not know why this does not work but something like
def checkLoadResult(self, result):
if (result == False):
self.page().mainFrame().setHtml ( "<html><head><h1>Not Found</h1></head>\
<body><p> Search at <a href='http://google.com'> google </a>\
</p></body> </html>")
does.
精彩评论