How to get QWebKit to display an image?

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

I call the following:

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

where webview is a QWebKit pointer

However, when I run the program, the image is not displayed. I run the program from the directory in which the image is located ... Why is it not displayed? It drives me crazy!

+6
qt image qwebkit
source share
4 answers

logo.png may be incorrectly resolved if the corresponding baseUrl is not specified.

Here is an example. Please note that the application must be launched from a directory containing the .png logo, but dummy.html does not have to exist. It is simply used to ensure 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(); } 
+10
source share
  • Check if Qt is with png support. You can also try a different image format.
  • Try using the absolute path. For me, this option works:

Code example:

 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 the code using QWebkit from my huge project, but here what I got. A project file named Images.rar. It is combined with MS Visual C ++. I successfully create code from this archive using Qt 4.6.2.

You can also watch the preview application from the standard Qt example folder. On my host, the path is:

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

Hope this helps!

+1
source share

The problem is security restrictions in WebKit.

AFAIK. The idea is to prevent HTML pages from the Internet from including / referencing files on the hard drive, and for some reason, calling setHtml() makes WebKit think that HTML should not have access to files on the disk (using the file:// scheme file:// ).

I think I worked on this, letting WebKit know that the HTML set really comes from the hard drive itself and should have access to local files accordingly.

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

It is not possible to test it here and now, but you can try to give baseUrl something like file://abcd .

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

Try linking to a PNG image on the Internet (for example, http://files.iconfactory.net/news/CandyBar.png ) and see if it appears. If so, PNG is supported, and the problem is related to security restrictions in WebKit.

+1
source share

I had the same problem, and finally it was developed.

Be sure to specify the correct path in QUrl to read the image correctly.

for example: the image file in C: /xxx/abc.png → QUrl should be something like C: / xxx / yyy → yyy the same directory with the image should be included.

During debugging in Qt, your current directory is the build directory! not in the debug / release folder ..

On exe, you get the real current directory by calling QDir :: current () .

+1
source share

All Articles