How to get QWebKit to display image?

只愿长相守 提交于 2019-12-21 12:17:26

问题


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:


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



回答2:


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.




回答3:


  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!




回答4:


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().



来源:https://stackoverflow.com/questions/2727080/how-to-get-qwebkit-to-display-image

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