PDA

View Full Version : QWebView sometimes not loading external resources



complexgeek
10th February 2010, 23:45
Hi.

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(QtCore.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.

wysota
19th February 2010, 10:58
Maybe try redirecting not in loadFinished() but elsewhere. Try making the signal-slot connection queued, so that the slot is executed when the application is already back in the event loop. It's possible that loadFinished() is connected to some other slot which is interfering with what you are trying to do. Delaying your actions a bit might be enough to solve the problem.

complexgeek
6th June 2011, 02:37
Thanks wysota.

After shelving this project for a number of reasons before picking it up again recently, your suggestion of using a queued connection worked perfectly!

I switched from using PyQt to PySide, but the fix was as simple as:
self.connect(self, QtCore.SIGNAL('loadFinished(bool)'), self.checkLoadResult, QtCore.Qt.QueuedConnection)