[PyKDE] QTextBrowser : strange loadResource behaviour

Phil Thompson phil at riverbankcomputing.co.uk
Sat Oct 21 10:52:26 BST 2006

On Saturday 21 October 2006 10:28 am, Pierre D. wrote:
> Hi
> I'm facing a strange bug with QTextBrowser, making it near of unusable for
> me with PyQt4.
> I need to display some HTML with images embedded. To do that, I've to
> create a new QTextBrowser-heriting class, and implement loadResource.
> I did so, and it works, but only for local pictures.
> Here is my smallest test code :
> #!/usr/bin/python
> import sys, httplib, os.path
> from PyQt4 import QtGui, QtCore
> class myTextBrowser(QtGui.QTextBrowser):
>     def loadResource (self, type, name):
>         return QtGui.QPixmap("test.jpg")
> if __name__ == "__main__":
>     app = QtGui.QApplication(sys.argv)
>     a = myTextBrowser()
>     a.document().setHtml("""Coucou :<br /><img src="test.jpg"/>""")
>     a.show()
>     sys.exit(app.exec_())
> This code just work as expected, it only display one "TypeError: invalid
> result type from myTextBrowser.loadResource()" on the console.
> Now, if I replace src="test.jpg" with src="http://somewhere/test.jpg" or
> any other http:// URL, it'll fail, displaying 10 times the message
> "TypeError: invalid result type from myTextBrowser.loadResource()"
> I tried the same code using C++, and it just worked.
> So I fear it's a PyQt issue :/

PyQt doesn't do automatic type conversion like C++ does. loadResource() is 
expected to return a QVariant - so the exception being raised is correctly 
telling you what the bug is in your code.


More information about the PyQt mailing list