[PyQt] doubt with QTextBrowser and QImage

David Boddie dboddie at trolltech.com
Tue Nov 27 18:55:36 GMT 2007

On Tue Nov 27 18:17:42 GMT 2007, Claudio A. Quezada wrote:

> The thing is that i´d like to generate matplotlib charts (or could be
> PyQWt) and pass them to QTextBrowser as html, but whitout having to
> save them to disc (could be as QImage or QPixmap lists). I think this
> maybe could be advantageous and faster (reduce hard disk writing to
> pass them as QImage object).

Perhaps. It's possibly less work to manage them if they're not on disk.

> I´m stucked right now, i was checking QTextBrowser and QTextEdit
> documentation and the only thing that maybe could do this is
> QTextBrowser.loadResource(), but i don´t get it...

Here's a way to add a resource to a text document so that any references
to it (either using the QTextFormat API or from HTML) show an image.

Let's assume you already have a document:

  document = QTextDocument()

And let's assume you obtained a QImage from somewhere:

  image = QImage(100, 100, QImage.Format_ARGB32)

You register the image with the document using a certain URL:

                       QUrl("matplotlib://image.png"), QVariant(image))

Now, any references to that URL in the document should result in an image
being displayed at that point. For example, here's how to insert a reference
to that image using a text cursor:

  imageFormat = QTextImageFormat()
  cursor = QTextCursor(document)

Or, once you have an editor, you can insert HTML to show it:

  editor = QTextEdit()
  editor.append('<img src="matplotlib://image.png">')

Hope this helps,

David Boddie
Lead Technical Writer, Trolltech ASA

More information about the PyQt mailing list