Daniel Donnelly
Daniel Donnelly

Reputation: 610

QWebEngineView will not load a local file, but will load perfectly a remote webpage

An MWE demoing the problem is this:

#include <QMainWindow>
#include <QApplication>
#include <QWidget>
#include <QWebEngineView>
#include <QGridLayout>

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

    auto w1 = new QWidget();
    w1->setLayout(new GridLayout());

    auto view = new QWebEngineView();
    view->load(QUrl("file://C:\\Users\\FruitfulApproach\\Desktop\\AbstractSpacecraft\\MathEnglishApp\\KaTeX_template.html"));
    view->show();
    w1->layout()->addWidget(view);
    w.setCentralWidget(w1);
    w.show();
    return a.exec();
}

KaTeX_template.html:

<!DOCTYPE html>
<html>
<head>
<title>MathJax TeX Test Page</title>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript" async
  src="https://example.com/mathjax/MathJax.js?config=TeX-AMS_CHTML">
</script>
</head>
<body>
When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$
</body>
</html>

You have to type in the correct file path in the first code listing.

Anyway, it's not the template, because I put some plain text inside of the template and the same thing happens.

What I expected was for QWebEngineView to be able to load a local file. I've tried playing around with the filepath string using forward instead of back-slashes, each change from what it is produce a "File not found" in the web view display.

What happens currently is that the QWebEngineView is just displaying blank white. When you right-click on the view you can try reload or view source and nothing happens.

I've tried loading an online webpage and that works.

Upvotes: 0

Views: 1319

Answers (3)

pthomaid
pthomaid

Reputation: 177

A nice alternative to the accepted answer is to use

QUrl::fromUserInput

which is more flexible and covers also network paths.

Upvotes: 1

Allen Kempe
Allen Kempe

Reputation: 73

QURL::fromLocalFile works under Windows but it does not on Linux! This can be readily reproduced in the Map example program by replacing the link to Google Maps with the path of an html file such as theApache2 file /var/www/html/index.html.

Upvotes: 0

eyllanesc
eyllanesc

Reputation: 244003

If you want to create a QUrl using a file path then use QUrl::fromLocalFile():

view->load(QUrl::fromLocalFile("C:\\Users\\FruitfulApproach\\Desktop\\AbstractSpacecraft\\MathEnglishApp\\KaTeX_template.html"));

Upvotes: 6

Related Questions