Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get QWebKit to display image?

Tags:

image

qt

qwebkit

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!

like image 689
Nathan Osman Avatar asked Apr 28 '10 05:04

Nathan Osman


1 Answers

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();
}
like image 150
baysmith Avatar answered Sep 22 '22 23:09

baysmith