3

I'm currently playing around with QWebEngineView in Qt 5.8 and I would like to load an index.html file from my .qrc file.

My .pro file looks like this:

TEMPLATE = app
TARGET = Launcher
QT += webenginewidgets
CONFIG += c++14

SOURCES += main.cpp

RESOURCES += \
    launcher.qrc

My main.cpp file looks like this:

#include <QApplication>
#include <QWebEngineView>

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

    QWebEngineView view;
    view.load(QUrl("qrc:/html/index.html"));
    view.resize(1024, 768);
    view.show();

    return a.exec();
}

In my project there is a launcher.qrc file:

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

Inside index.html I just added the text Hello World without anything else.

When I start the application I just get a "Website not reacheable" error screen.
I then googled around and tried several different attempts to specify the resource url to my QWebEngineView:

view.setUrl(QUrl("qrc:/html/index.html")); // Same error page
view.page()->setUrl(QUrl("qrc:/html/index.html")); // Same error page
view.page()->load(QUrl("qrc:/html/index.html")); // Same error page

If I change the resource url from qrc:/html/index.html to :/html/index.html I don't get this error page anymore but a blank page instead. If I then rightclick the window and select "View page source" the page source is empty, too.

I recently got this working with a fresh Qt Quick Application created with Qt Creator 4.2.2 using the same qrc:... url.
Now I created a Qt Widgets Application and it doesn't work anymore.
What am I missing here?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
TorbenJ
  • 4,462
  • 11
  • 47
  • 84
  • 1
    Works fine for me. Any compile / application output generated indicating anything out of the ordinary? – deW1 Apr 30 '17 at 14:49
  • 1
    Can you try with the most simple website you can think of?

    Heading

    Something

    – deW1 Apr 30 '17 at 14:58
  • It doesn't work regardless of what I put into this file. It seems that the file cannot be found. If I try to open and read the file using QFile and QTextStream the application output says "QIODevice::read (QFile, "qrc:\html\index.html"): device not open". If I create a new Qt Quick project and use the created .qrc file everything works fine. I don't know why this works and my original Qt Widgets Application does not. – TorbenJ Apr 30 '17 at 15:40
  • 1
    is the html file located in your folder where the resource file is? also try qrc:///html/index.html what does the full qt += statement look like? – deW1 Apr 30 '17 at 15:46
  • Yes it is. I have just five files in the source directory: index.html, Launcher.pro, Launcher.pro.user, launcher.qrc and main.cpp. Your suggested url doesn't work too :( – TorbenJ Apr 30 '17 at 15:50
  • 1
    Forget what I just said. I coincidentally cleaned the project and hit "Run qmake" and then ran the project again. This time it worked with any of the three urls. That's so frustrating. Thanks for your assistance @deW1 – TorbenJ Apr 30 '17 at 15:54
  • 1
    @TorbenJonas, please put that as an answer. Although it might seem trivial, it could actually save you a lot of time trying to get it work. – scopchanov Jun 14 '17 at 21:55

1 Answers1

3

As suggested I will put the solution from the comments above as an answer for future users having the same issue.

"[...] I coincidentally cleaned the project and hit "Run qmake" and then ran the project again. This time it worked with any of the three urls. That's so frustrating. Thanks for your assistance @deW1"

TorbenJ
  • 4,462
  • 11
  • 47
  • 84