1

Hello I am trying to set the QWebEngine URL to an index.html file that is placed in the working directory. I am trying to use the file by setting the URL to ./index.html but it cant seem to find the file.

Here is where my files are placed

  • content (Work directory)

    • main.cpp
    • content.pro
    • index.html

How can i open index.html through the QWebEngine without using the full system path?

here is my code

#include <QApplication>
#include <QWebEngineView>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);

    QWebEngineView view;
    view.setUrl(QUrl(QStringLiteral("file:///./index.html")));
    view.resize(1024, 750);
    view.show();

    return app.exec();
}
Noopty
  • 167
  • 2
  • 12
  • For whoever wants to do something similar what I did is that I changed my default build directory from the build settings and then followed @p-a-o-l-o answer. This worked fine with me, but I am going to try to use qrc to do something similar because it seems to me that it is an ideal practice for Qt. – Noopty Mar 14 '18 at 05:56

3 Answers3

3

Try moving the html file to your project build directory (you're currently keeping it inside the source directory). Then you can build your URL this way:

QUrl url = QUrl::fromLocalFile(QDir::currentPath() + "/index.html");

and set it to the view:

QWebEngineView view;
view.setUrl(url);
view.resize(1024, 750);
view.show();
p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
  • Yeah, I was afraid that it would just point to the build directory but I guess I can initially change the default build directory elsewhere. – Noopty Mar 13 '18 at 11:58
  • So I did what you suggested and for some reason it sis giving me a file not found error. I assumed that I should have included QStringLiteral for the URL? I added it here view.setUrl(QStringLiteral(url)); But I am getting an error saying "Invalid template argument for 'QStaticStringData'" and a bunch of missing parenthesis errors. – Noopty Mar 13 '18 at 12:03
  • @Yaseen please try using `QUrl::fromLocalFile` as I did in my example. – p-a-o-l-o Mar 13 '18 at 12:58
  • Yep, I did use the same line `QUrl url = QUrl::fromLocalFile(QDir::currentPath() + "/index.html"); QWebEngineView view; view.setUrl(url); view.resize(1024, 750); view.show();` It still cant find the file for some reason – Noopty Mar 13 '18 at 13:21
  • @Yaseen please debug your code to see which path is returned by `QDir::currentPath` then double check the file is there. – p-a-o-l-o Mar 13 '18 at 13:25
  • you were right, I did misplace the file it was in the debug folder that has the .exe file but it only needed to be in the actual build folder which is a level higher, I moved it there and everything is working fine now. Thanks! – Noopty Mar 14 '18 at 05:54
0

From http://doc.qt.io/qt-5/qurl.html

qDebug() << QUrl("main.qml").isRelative();          // true: no scheme
qDebug() << QUrl("qml/main.qml").isRelative();      // true: no scheme
qDebug() << QUrl("file:main.qml").isRelative();     // false: has "file" scheme
qDebug() << QUrl("file:qml/main.qml").isRelative(); // false: has "file" scheme

Try: view.setUrl(QUrl(QStringLiteral("index.html")));

L.S.
  • 476
  • 4
  • 10
0

As p-a-o-l-o pointed out in his answer, you're likely building out-of-source, so your index.html file has to be in the folder where content.exe is created, not in the source folder.

To make this less complicated, and safer, Qt supports embedding files in the .exe via Qt Resource files (.qrc). These can easily be created in Qt Creator, and once added to the project, the embedded files are accessed via a qrc:/// prefix.

So in your sample code, after adding a .qrc file to your project and adding index.html to it, you would adjust your code like this:

view.setUrl(QUrl(QStringLiteral("qrc:///index.html")));

This has the advantage of working regardless of build type or location, and it's a lot simpler than trying to add a file copy step to your project file (or to manually copy the file each time)

Romain Pokrzywka
  • 200
  • 1
  • 12
  • qrc would actually be perfect if it worked with QUrl I just followed your instructions and added the file from ./HtmlViz/index.html but it still cant find it. do i need to keep all files in the folder that contains the qrc? – Noopty Mar 13 '18 at 12:11