如何让QWebKit显示图像? [英] How to get QWebKit to display image?
问题描述
好的,我在同一个目录中有一个Qt可执行文件 logo.png
。
Okay, I have a Qt executable in the same directory as a file logo.png
.
I请拨打以下电话:
QString msg("<html><body><img src='logo.png' /></body></html>");
webView->setHtml(msg);
其中 webview
是 QWebKit
指针
但是,当我执行程序时,图像不会显示。我正在从图像所在的目录执行程序... 为什么不显示?这让我疯了!
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!
推荐答案
如果没有给出适当的baseUrl,可能无法正确解析logo.png。
The logo.png may not be resolved properly if a appropriate baseUrl is not given.
这是一个例子。请注意,必须从包含logo.png的目录运行应用程序,但dummy.html不需要存在。它仅用于提供适当的baseUrl。
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();
}
这篇关于如何让QWebKit显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!