1

I have a Qt Widgets project that employs QWebEngineView to show a static HTML page. That page will show an image that needs to be "packed" with the Qt executable/installer. Can I use the QRC file to add an image and refer to it from the HTML file?

I (pathetically) tried to access the image using the "qrc:/image..." notation, but I understand perfectly that the QWebEngineView shows an embedded browser that have no relation to the project's resources. An alternative way?

jpnurmi
  • 5,716
  • 2
  • 21
  • 37
rubmz
  • 1,947
  • 5
  • 27
  • 49
  • 1
    Not exactly understand what you want, but can you load the image to temporary file and to point it as a source in HTML? – ilotXXI Dec 08 '16 at 09:21
  • could be a good solution as well. Only that in this case I need to deploy the html + picture to some temporary location. Like lesyk's answer – rubmz Dec 08 '16 at 13:45
  • You can use `QTemporaryFile`, `QTemporaryDir` or `QDesktopServices::TempLocation` as utility for temporary location generation. One more way is to embed the image into HTML using Base64 encoding but it can make your HTMLs large and I am not sure that `QWebEngineView` supports it. – ilotXXI Dec 08 '16 at 19:39

1 Answers1

2

For Qt 5.6 in main.cpp:

...
QString helpHTMLFile = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
helpHTMLFile.append(QDir::separator());
helpHTMLFile.append("index.html");
QFile(helpHTMLFile).remove();
QFile(":/index.html").copy(helpHTMLFile);

QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("pathToFile", "file:///"+helpHTMLFile);

QString logo = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
logo.append(QDir::separator());
logo.append("logo.svg");
QFile(logo).remove();
QFile(":/logo.svg").copy(logo);
...

In html:

...
<img src="logo.svg" class="loginFooterImg">
...
lesyk
  • 3,979
  • 3
  • 25
  • 39