QWebView won't load file from qrc resource
I have a .qrc file in my project, which is basically the default html5 template project, and my web view cannot load my index.html file via qrc. the qrc resources are accessible when I try to open them with QFile, but if I use the exact same qrc url with my web view, I get nothing displayed. I've observed this behavior with Qt 5.2.1 and with 5.3.0. I've used the "Copy Resource Path to Clipboard" option in the qrc editor, and the resulting path works with QFile, but not with the web view. I'd expect that QWebView and QGraphicsWebView use QFile on the back end, but for some reason, it just won't show my html page.
is there something special about QWebView or QGraphicsWebView that prevents qrc resources from working? is there another step I'm missing?
Just an educated guess but did you try with
Hope it helps
unfortunately, I tried with every combination of:
maybe a few others. I've tried so many things, I've lost track of all of them.
works just fine, but
Have you tried ?
EDIT: Did you check what QUrl(":/html/index.html") returns
I'll have to pick this up again on tuesday, as my work week is over.
right-clicking on index.html in the qrc editor, and selecting "Copy Resource Path to Clipboard" gives "://index.html"
for (QFileInfo& e : dir.entryInfoList())
std::cout << "File: " << e.absoluteFilePath() << std::endl;
produces the following output:
I have tried both versions of the path, and neither works with the web view, even with QUrl::fromLocalFile(), but both work with QFile.
QUrl(":/index.html") shows "://://index.html" and QUrl::fromLocalFile(":/index.html") shows "file://:/index.html" in the debugger locals window.
Changing the url to "qrc:/index.html" causes both the web view and QFile to fail to open the file.
Can one of you try this to see if you get the same results?
steps to reproduce:
- create a new html5 project from the default qt creator template.
- add a qrc file to your project.
- add your index.html file to the qrc file.
- use the qrc path instead of the local file path when loading the web view.
- run and watch it fail to load the html file.
I looked at QUrl source code.
It does not support qrc: prefix.
A solution could be to copy index.html from the resource to a temporary file and then use QUrl::fromLocalFile("file://temp_path/index.html");
I wouldn't really consider that a solution. The web view has to load additional resources (css, js, images), based on the content of the html, and I'd have to literally copy everything to the temp folder. Once this project starts to get big, that's simply not an option.
I agree with you. It is not convenient. But I don't see the other way.
I see two issues with putting all html related files as the resources:
When you put all html, js, etc files in the executable file as the resources, how are you going to manage the links inside these html, js, etc files?
If you will put a lot of resources in the executable file it will make the file big and relatively slow to load.
can you suggest alternatives, so that the content of my app isn't just sitting there on the user's computer to manipulate as they please?
I think Qt assistant maybe a good example. Take a look on how they provide and show bunch of html files.
Or something simple like this
Extract all files to OS dependent cache directory on first run or after installation
QString cacheDir =
Load the files from the cache directory
Provide checksum for the files if you need to protect an app from loading the modified files.
Thanks for the suggestion. I used exactly what you suggested (except TempLocation instead of CacheLocation), and recursively copied the contents of the resources to the destination folder. Everything works, and files are deleted automatically on program exit on my target platform (linux).
Glad it works for you.
Could you please prepend "[SOLVED]" to the title of your post. It will help other good people to find your solution.
If you're reading from a .qrc resource file you have to run qmake ("Build->Run qmake" in Qt Creator) before it will be available.
It is possible to solve this problem in a simple way you wanted. The correct usage of URLs here is the following:
So, just use "qrc:///" as a prefix.
For me, using the
qrc:/html/index.html) along with cleaning the project and running qmake worked. But it didn't work by simply using
:/html/index.htmlor before cleaning the project and running qmake.