Hi All,
I´m using a QTextBrowser to display an external html document (and its image resources) which is placed in the same directory as the application. Everything works fine except that images are not displayed properly. Instead of the actual picture there is a "missing image" icon.

I tried different image formats and Qt versions, without success.

If I type in the absolute file path of the image it is displayed fine. But that´s not what I´d like to do as I can´t share my application then.

This is the part which loads the html file into the textbrowser:

QFile file(QApplication::applicationDirPath().append("/test.html"));
if(!file.open(QIODevice::ReadWrite|QIODevice::Text))
    return;

QTextStream in(&file);
ui->textBrowser->setHtml(in.readAll());
file.close();

And this is my html document:

<!doctype html>
<html>
    <img src="test.png">
    <p>paragraph which contains some text</p>
</html>

Does anybody has an idea why it doesn´t display the image?

Thanks in advance!

Recommended Answers

All 3 Replies

If you look at the documentation for setHtml function, you will see that it has a second parameter (defaulted in your case) to specify the base URL (or file path) from which to get any external objects (stylesheets or images) referenced in the html. Here is a quote of the reference:

"External objects such as stylesheets or images referenced in the HTML document are located relative to baseUrl."

As rubberman often says: RTFM! (Read The Manual!)

Hello Mike,
Thanks for the reply. But the sethtml() function i'm using here is the one specified for QTextDocument and it does not take any second parameter. Since QtextBrowser derives from this it will use th same sethtml() method so there is no specifying of url.

When you open the HTML document in your browser does it show the image? Where is the image located relative to the HTML document? I have not used Qt but doing some basic googling it looks like you might need to use QTextDocument::loadResource to added images to the document.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.