Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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"));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading