September 22, 2009

Displaying html with animated gifs

When consulting for Hyves, we wanted a light-weight widget that displays html and animated gifs (for smileys) for the chat client and for tooltips. The option to use Qt/WebKit was out since we intended to create numerous instances of this widget (it consumed lots of memory) and the rendering was not as fast we would like. QLabel does not support animated gifs, so that was not an option either.

Instead of writing a basic html parser, layouter and renderer, I thought I can somehow trick QTextDocument into displaying animated gifs. You can see the basic idea in this kb but to get it working with animations wasn’t easy. One good thing about adding the feature to QTextDocument is that you can set it on QTextBrowser and QTextEdit.

Without further ado, get the code here. Label is a richtext label that does not support links or selection. TextBrowser is a browser that supports selection and links. TextEdit is an editor with support for undo/redo.

P.S. Thanks to Hyves for making it possible to actually publish this code; they released most of their code as GPL.

4 Responses to “Displaying html with animated gifs”

  1. JohnFlux says:

    Does QLabel support animated images?

  2. girish says:

    @JohnFlux, No, QLabel does not not supported animated images. This is the reason for this code :)

  3. Jason says:

    I was able to get a segfault with “Position 21 out of range” or something like that. I just copy and pasted things around a lot.

  4. girish says:

    @jason, you probably cut and paste the gif animation itself. Currently, all the classes do not work if there are multiple animations with the _same_ source. i.e you can display two animations x.gif and y.gif but not x.gif twice (which is what happens on copy/paste). This limitation can be fixed, I just have not done it since we didn’t need it. FWIW, this is documented in textdocument.h :)

Leave a comment



(unmoderated)

    (or)    


(moderated, email will not be published)