PDA

View Full Version : QTextEdit and delayed image loading



Vladimir
26th April 2007, 10:10
Hello All !

I'm using QTextEdit to display local text with embedded images from the internet. In the constructor I'm setting the text using setHtml(), and then I'm calling textEdit->document()->addResource() when each image arrives. After adding each image I want QTextEdit to immediately show it, the only way I've found to do it is to call textEdit->setHtml(textEdit->toHtml()), but this is really slow and causes flicker. Are there any better ways to do it ?

Methedrine
26th April 2007, 19:07
I guess you could write a proxy-class which downloads the images and displays them once they were loaded. This would allow you to cache the images and prevent them from reloading all the time. Even though I don't know if Qt might do it that way already...

jpn
26th April 2007, 19:46
Maybe QTextDocument::loadResource() would do the trick? Requires subclassing, though. At least worth trying. ;)

Vladimir
26th April 2007, 21:43
Thanks, I've tried it. But QTextEdit requests resource immediately after I'm setting the text. What I want is the ability to add image later (when it arrives from the server) and to get this image correctly displayed in QTextEdit without re-rendering the whole page. If I'm returning empty QVariant() from loadResource() while the image is still not available then when the image finally arrives it does not become visible in QTextEdit, or (if I resize the window) becomes incorrectly positioned.

jpn
27th April 2007, 07:33
Thanks, I've tried it. But QTextEdit requests resource immediately after I'm setting the text. What I want is the ability to add image later (when it arrives from the server) and to get this image correctly displayed in QTextEdit without re-rendering the whole page. If I'm returning empty QVariant() from loadResource() while the image is still not available then when the image finally arrives it does not become visible in QTextEdit, or (if I resize the window) becomes incorrectly positioned.
Yes, I understood the problem, but that's not exactly what I meant with using QTextDocument::loadResource(). What I meant was using the built-in QTextDocument::loadResource() after the image has been received. It should load up the resource just fine.

The only problem is updating the document layout. Basically we want QTextEditPrivate::relayoutDocument() to somehow get called. I'm quite surprised there is no direct way to do this. I would have expected to find something relevant in QAbstractTextDocumentLayout. I have found a hackish way to trigger update indirectly, by using QTextEdit::setLineWrapColumnOrWidth(). I'm not sure if it's that good idea relying on that because it might be a subject of change, but I have still attached an example of how to get it working for now.

Unless someone is able to come up with a proper way of updating the document layout, I'd suggest reporting a suggestion to the Task-Tracker (http://www.trolltech.com/developer/task-tracker).

Vladimir
27th April 2007, 08:13
Thanks a lot, your solution works ! If nobody will propose more direct solution I'll report this to the tracker.