QWebEngineView不会加载本地文件,但会完美加载远程网页
QWebEngineView will not load a local file, but will load perfectly a remote webpage
演示问题的 MWE 是这样的:
#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>
您必须在第一个代码清单中输入正确的文件路径。
无论如何,这不是模板,因为我在模板中放了一些纯文本,结果也是一样。
我期望 QWebEngineView 能够加载本地文件。我试过使用正斜杠而不是反斜杠来处理文件路径字符串,每次更改都会在 Web 视图显示中产生“找不到文件”。
当前发生的情况是 QWebEngineView 仅显示空白。当您右键单击视图时,您可以尝试重新加载或查看源代码,但没有任何反应。
我试过加载在线网页,效果很好。
如果您想使用文件路径创建 QUrl,请使用 QUrl::fromLocalFile():
view->load(QUrl::fromLocalFile("C:\Users\FruitfulApproach\Desktop\AbstractSpacecraft\MathEnglishApp\KaTeX_template.html"));
演示问题的 MWE 是这样的:
#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>
您必须在第一个代码清单中输入正确的文件路径。
无论如何,这不是模板,因为我在模板中放了一些纯文本,结果也是一样。
我期望 QWebEngineView 能够加载本地文件。我试过使用正斜杠而不是反斜杠来处理文件路径字符串,每次更改都会在 Web 视图显示中产生“找不到文件”。
当前发生的情况是 QWebEngineView 仅显示空白。当您右键单击视图时,您可以尝试重新加载或查看源代码,但没有任何反应。
我试过加载在线网页,效果很好。
如果您想使用文件路径创建 QUrl,请使用 QUrl::fromLocalFile():
view->load(QUrl::fromLocalFile("C:\Users\FruitfulApproach\Desktop\AbstractSpacecraft\MathEnglishApp\KaTeX_template.html"));