【问题标题】:QWebEngineView will not load a local file, but will load perfectly a remote webpageQWebEngineView 不会加载本地文件,但会完美加载远程网页
【发布时间】:2022-01-03 12:00:20
【问题描述】:

演示问题的 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 只是显示空白。当您右键单击视图时,您可以尝试重新加载或查看源代码,但没有任何反应。

我已尝试加载在线网页,并且成功了。

【问题讨论】:

    标签: c++ qt qwebengineview


    【解决方案1】:

    如果要使用文件路径创建 QUrl,请使用 QUrl::fromLocalFile():

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      相关资源
      最近更新 更多