How to display a built-in html Qt WebView

I am trying to get Qt WebView to display an html file that is embedded in the Qt resource, but I cannot get it to work. I have created a new application Qt Quick and added a simple qml file:

import QtQuick 2.0 import QtWebKit 3.0 Rectangle { id: content width: 800 height: 600 color: "black" WebView { id: webView anchors.fill: parent url: "qrc:/res/test.html" } } 

Then I created (using the constructor) resource file that looks like this:

 <RCC> <qresource prefix="/res"> <file>test.html</file> </qresource> </RCC> > <RCC> <qresource prefix="/res"> <file>test.html</file> </qresource> </RCC> 

and I created a simple file test.html (in the same directory as the file .qrc):

 <html> <head><title>Hello</title></head> <body> <h1>Hello World!</h1> </body> </html> h1> <html> <head><title>Hello</title></head> <body> <h1>Hello World!</h1> </body> </html> 

Result - just a blank white window. If I use a normal url No ( http://www.stackoverflow.com ) in qml file as a URL-addresses, it works - page is displayed. If I use the name of an embedded image ( qrc:/qt-project.org/mac/cursors/images/pluscursor.png ), the image will appear.

I think that really added html file (it is, at least, is listed when I list the embedded assets), but my understanding of the Qt Resource System is limited, so I could well misunderstand something fundamental.

Can you tell me someone that I'm doing wrong?

Update: I have verified that the behavior is the same, if I try to tell the web browser to download the URL-address of a C ++. I also checked that the resource is actually built - I can open and read resource using QResource. In addition, it does not seem to Qt5: http://qt-project.org/forums/viewthread/18181 (someone with a similar problem with Qt 4.8).

+4
source share
7 answers

Well, that's how I decided to solve it. In the end, I used a simple widget instead of the qml interface.

Then I had to first read the first page html of resources and to provide a base url. Thereafter, subsequent pages and the resources that are embedded as resources are loaded properly.

Here is the code in question:

 QResource res(":/html/index.html"); ui->webView->setHtml(reinterpret_cast<const char *>(res.data()), QUrl("qrc:/html/")); index.html"); QResource res(":/html/index.html"); ui->webView->setHtml(reinterpret_cast<const char *>(res.data()), QUrl("qrc:/html/")); 

where webView - QWebView . My .qrc file looks like this:

 <RCC> <qresource prefix="/"> <file>html/index.html</file> </qresource> </RCC> file> <RCC> <qresource prefix="/"> <file>html/index.html</file> </qresource> </RCC> 

It works in Qt 5.5.0.

0
source

First make sure that the resource file is compiled correctly in the compilation folder (file .RCC or qrc_<Resource name>.cpp )
Second, make sure your path is correct (in my case qrc:/images/resource/html/test.html ). You can open the QRC file and right-click on the html file, and then click Copy Resource Path to Clipboard .
Third, note that more time is required for some of the URL-addresses for a complete download.

Finally, I test this script, and it works very well (using Qt 5.1.0)

Good luck - S.M.Musavi

+2
source

I can not open resource from qrc directly in WebView (I've tried all the options url). What I'm doing now - is to copy the file to the local directory the temp, and then opening.

 QString helpHTMLFile = QStandardPaths::writableLocation(QStandardPaths::TempLocation); helpHTMLFile.append(QDir::separator()); helpHTMLFile.append("software_manual.html"); QFile(helpHTMLFile).remove(); QFile(":/software_manual.html").copy(helpHTMLFile); QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("pathToFile", "file:///"+helpHTMLFile); :: TempLocation); QString helpHTMLFile = QStandardPaths::writableLocation(QStandardPaths::TempLocation); helpHTMLFile.append(QDir::separator()); helpHTMLFile.append("software_manual.html"); QFile(helpHTMLFile).remove(); QFile(":/software_manual.html").copy(helpHTMLFile); QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("pathToFile", "file:///"+helpHTMLFile); ; QString helpHTMLFile = QStandardPaths::writableLocation(QStandardPaths::TempLocation); helpHTMLFile.append(QDir::separator()); helpHTMLFile.append("software_manual.html"); QFile(helpHTMLFile).remove(); QFile(":/software_manual.html").copy(helpHTMLFile); QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("pathToFile", "file:///"+helpHTMLFile); copy (helpHTMLFile);. QString helpHTMLFile = QStandardPaths::writableLocation(QStandardPaths::TempLocation); helpHTMLFile.append(QDir::separator()); helpHTMLFile.append("software_manual.html"); QFile(helpHTMLFile).remove(); QFile(":/software_manual.html").copy(helpHTMLFile); QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("pathToFile", "file:///"+helpHTMLFile); 

Then QML looks like this:

 WebView { anchors.fill: parent url: pathToFile } 
+2
source

why not just use

 url: "res/test.html" 

instead

 url: "qrc:/res/test.html" 

?

Then you will not need to use the resource files

0
source

You can put the following lines just after the QApplication a (argc, argv);

 QtWebView::initialize(); 
0
source

Support for the URL-addresses embedded resource usage through a web engine is available in the Qt last, at least for WebEngineView in QML. I could not find the official documentation about this, but one official sample loads a page of resources:

 WebEngineView { url: "qrc:/index.html" ... } 

It works for me with Qt 5.8.0 .

0
source

Just use the Path-To-File and, if you have kept it in the project directory, simply enter the name of the file url: "/res/test.html"

& If the file is in the directory of the project url: "test.html"

-1
source

All Articles