TorbenJ
TorbenJ

Reputation: 4582

QWebEngineView - Load html from resources

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?

Upvotes: 3

Views: 5147

Answers (1)

TorbenJ
TorbenJ

Reputation: 4582

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"

Upvotes: 3

Related Questions