PDA

View Full Version : How to display images in QTextBrowser ?



probine
12th January 2007, 00:48
I want to display images in my QTextBRowser. Those images are from different websites.

So far, I am able to connect to those sites with a QHttp and so on...

But the pages are displayed, and every time an image should appear, I see a symbol, but not the images.

This is what the debug prints:

QTextBrowser: Cannot open 'http://localhost/website/images/logo.png' for reading
QFSFileEngine::open: No file name specified

Why?

camel
12th January 2007, 08:58
But the pages are displayed, and every time an image should appear, I see a symbol, but not the images.

This is what the debug prints:

QTextBrowser: Cannot open 'http://localhost/website/images/logo.png' for reading
QFSFileEngine::open: No file name specified

Why?

I would guess that you try to give QTextBrowser some html such as "<img src="XXX" />", right?

The problem in that case is that, QTextBrowser is no WebBrowser and does not know how to handle http.

One possible aproach that I can see (but have not tested) is to create a new class inherrited from QTextBrowser and overload the loadResource (http://doc.trolltech.com/4.2/qtextbrowser.html#loadResource) function in a way where you check for the names you are interested in and resolve them.
For example like this (again not tested)


QVariant MyBrowser::loadResource(int type, const QUrl &name)
{
if (type == QTextDocument::ImageResource
&& name.scheme() == QLatin1String("mypics")) {
QImage correctImage;
//lookup the correct QImage from a cache
return QVariant::fromValue(correctImage);
} else {
return QTextBrowser::loadResource(type, name);
}
}

Then you should be able to show images when you call


myTextBrowser.append(QLatin1String("<img src=\"mypics://theCurrentPicture.png\" />"));



Disclaimer: Shot from the Hip, not tested...might be completely the wrong track