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

Time:11-25

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.

CodePudding user response:

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"));
  • Related