PDA

View Full Version : Loading images in QTextBrowser



nisha0609
2nd January 2007, 10:15
hello all!

I need to know how to load and insert images in QTextBrowser. Using the code below i load the image dynamically from an url eg. http://something/images/image1.gif but, stuck up with how to insert it in the QTextBroswer with the other content of the document using QTextCursor.

QUrl url(iurl);
QVariant cimg = teContent->loadResource(QTextDocument::ImageResource,url);
QImage im = cimg.value<QImage>();


I need to know how to proceed to insert the image? there is a function in QTextCursor to insertImage() which takes an object of QTextImageFormat as the argument.. How to associate the image object with the QTextImageFormat?

Plz help.
Nisha

wysota
3rd January 2007, 09:44
Quoting the docs:

Images
Images in QTextDocument are represented by text fragments that reference external images via the resource mechanism. Images are created using the cursor interface, and can be modified later by changing the character format of the image's text fragment:

if (fragment.isValid()) {
QTextImageFormat newImageFormat = fragment.charFormat().toImageFormat();

if (newImageFormat.isValid()) {
newImageFormat.setName(":/images/newimage.png");
QTextCursor helper = cursor;

helper.setPosition(fragment.position());
helper.setPosition(fragment.position() + fragment.length(),
QTextCursor::KeepAnchor);
helper.setCharFormat(newImageFormat);
}
}
The fragment that represents the image can be found by iterating over the fragments in the text block that contains the image.