【问题标题】:How to load an HTML page in Qt5 WebEngineView using utf-8 encoding?如何使用 utf-8 编码在 Qt5 WebEngineView 中加载 HTML 页面?
【发布时间】:2019-12-16 12:07:39
【问题描述】:

我有一个 html 页面,我正在使用 QWebEngineView 将其加载到我的 Qt5 应用程序中,如下所示:

QWebEngineView* webEngineView = new QWebEngineView();
webEngineView->setUrl("index.html");

我在我的 html 文件中使用 UTF-8 字符,例如“...”(#x2026;在 html 中),显示为“?”在应用程序中。

html 文件包含 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" 和我尝试使用 webEngineView->settings()->setDefaultTextEncoding("utf-8"); 无济于事。

谁能帮我显示这些字符?

【问题讨论】:

  • 您还应该显示包含您的 index.html。
  • 感谢您的建议。当我试图为我的 index.html 制作一个可行的小例子时,我意识到问题不在setUrl() 中,而是在我的 JS 文件中的一个 loalisation 函数中,它将编码设置为 ASCII。所以在完全错误的地方寻找解决方案:)
  • 这是我的个人经历,当我尝试创建 stackoverflow.com/help/minimal-reproducible-example 时,大部分时间这个过程本身解决了我的疑问/问题。

标签: html c++ encoding qt5 qwebengineview


【解决方案1】:

我发现问题不在于 setUrl() 方法,而在于我的 JS 文件中的本地化函数。该函数将编码显式设置为 ASCII 而不是 UTF-8。

QWebEngineView 的setUrl() 方法似乎使用正确的编码加载了 html 文件,而没有在 C++ 代码中明确指定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-06
    • 2017-06-20
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 2019-09-22
    相关资源
    最近更新 更多