How to get QWebKit to display image?

社会主义新天地 提交于 2019-12-04 04:47:14

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();
}

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.

  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!

Ahan

I was having the same problem, and finally worked it out.

Be sure to give correct path in QUrl, to get image read properly

eg: image file on C:/xxx/abc.png -> QUrl has to be something like C:/xxx/yyy -> yyy must be on the same dir with image.

While debugging on Qt your current dir is build directory! not on debug/release dir..

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!