views:

342

answers:

3

Okay, I have a Qt executable in the same directory as a file logo.png.

I call the following:

QString msg("<html><body><img src='logo.png' /></body></html>");

webView->setHtml(msg);

where webview is the QWebKit pointer

However, when I execute the program, the image does not display. I am executing the program from the directory that the image is in... why won't it display? This is driving me nuts!

+1  A: 
  1. Check, if Qt build with png support. Also you may try other image format.
  2. Try using absolute path. For me that variant works:

Sample code:

if (isPictureDirExists)
    text.replace(QString("src=\""), QString("src=\"%1/img/")
        .arg(conf.absImgFolder), Qt::CaseInsensitive);

QString html = QString("<html><head><meta Content=\"Text/html; " \
                       "CHARSET=Windows-1251\"></head><body>%1</body>" \
                       "</html>").arg(text);
webView->setHtml(html);

Good luck.

Added: I tried to scratch code with QWebkit from my huge project, and here is what I have got. Project file called Images.rar. It's combined with MS Visual C++. I build code from that archive successfully with Qt 4.6.2.

Also you may look at the previewer app from the standard Qt example folder. On my host the path is:

C:\Qt\4.6.2\examples\webkit\previewer 

Hope it help!

mosg
+1  A: 

The problem is security restrictions within WebKit.

AFAIK the idea behind that is to prevent HTML pages from the internet to include/reference files on the hard disk, and for some reason a call too setHtml() makes WebKit think that the HTML must not be allowed to access files on the disk (using the file:// schema).

I think I worked around this by letting WebKit know that the set HTML indeed comes from the hard disk itself and should be allowed to access local files respectively.

void QWebView::setHtml ( const QString & html, const QUrl & baseUrl = QUrl() )

Can't test it here and now, but you could try to give baseUrl something like file://abcd.

Also, lack of PNG support might be a problem, so you should check if you have PNG support enabled in your Qt build.

Try to reference to a PNG image on the web (for instance http://files.iconfactory.net/news/CandyBar.png), and see if it shows up. If it does, PNG is supported and the problem is security restrictions within WebKit.

BastiBense
Tried accessing an HTTP PNG file - no problems with that.
George Edison
Take a look at Qt WebKit's security policies. I think they have some documentation about that by now.
BastiBense
+1: Using the `baseUrl` argument works but only if I use an absolute path it seems. Annoyingly, passing `QUrl::fromLocalFile( "." )` as `baseUrl` does not work (despite `QUrl.valid()` returning `true` for that particular `QUrl`).
Troubadour
A: 

The logo.png may not be resolved properly if a appropriate baseUrl is not given.

Here is an example. Note that the application must be run from the directory containing logo.png, but the dummy.html need not exist. It is just used to provide a proper baseUrl.

#include <QtCore>
#include <QtGui>
#include <QtWebKit>

int main(int argc, char * argv[])
{
    QApplication app(argc, argv);

    QUrl baseUrl = QUrl::fromLocalFile(QDir::current().absoluteFilePath("dummy.html"));

    QString msg("<html><body><img src='logo.png' /></body></html>");

    QWebView *webView = new QWebView;
    webView->setHtml(msg, baseUrl);

    webView->show();

    return app.exec();
}
baysmith
I'll try it when I get the chance.
George Edison
Grrr, `Qdir::current()`, why didn't I think of that? :)
Troubadour