View Full Version : How to display a html map in a Qt widget?

5th July 2012, 07:14
#include <QtCore>
#include <QtGui>
#include <QtWebKit>

int main (int argc, char * argv[])
QApplication app (argc, argv);

QUrl baseUrl = QUrl::fromLocalFile (QDir::current().absoluteFilePath ("file:///home/anisha/Desktop/ogmap.html"));

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

QWebView *webView = new QWebView;
webView->setHtml(msg, baseUrl);

return app.exec();

This displays a small logo in the widget, nothing else. I am not interested in any logo.
When I open that html file through Konqueror/Firefox, map gets displayed.
What wrong am I doing here? I want the map to be displayed in that widget.

5th July 2012, 08:11
You see a logo because that's the HTML you gave the QWebView to display. The HTML does not come from the base URL; that's where relative references in the HTML are resolved relative to.

If you wanted the QWebview to show the ogmap.html file then you should give QWebView::setUrl() the URL to that file. At line 9 you take a roundabout path to try to create the file:// URL you started with :confused: Just:

QUrl url("file:///home/anisha/Desktop/ogmap.html");
// OR QUrl url = QUrl::fromLocalFile("/home/anisha/Desktop/ogmap.html");
QWebView view;

should do it.

5th July 2012, 08:15
Very thankful to you for that.
So, I was using the wrong function: `setHtml`

Your method worked flawlessly. :hattip: