QWebView可以从Qt资源文件中加载*.js并运行它们吗?
Can QWebView load *.js from Qt resource files and run them?
我有qrc文件,看起来像这样:
<qresource prefix="/web">
<file alias="assets.js">../web/assets.js</file>
<file alias="index.html">../web/index.html</file>
</qresource>
assets.js刚刚添加了allert弹出的功能:
function myFunction()
{
window.alert("Hello from assets.js");
}
在index.html中添加另一个javascript用于警报弹出,加载assets.js并添加2个按钮。第一个是从外部javascript文件(assets.js)调用窗口弹出,第二个调用嵌入到index.html文件中的javascript:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
function localFunction()
{
window.alert('HTML loaded');
}
</script>
<script src="assets.js"></script>
<button onclick="myFunction()">External JS</button>
<button onclick="localFunction()">Local JS</button>
</body>
</html>
现在当我试图加载index.html在qtwebkit:
webView->load(QUrl(QStringLiteral("qrc:/web/index.html")));
我可以看到index.html已经加载好了(我可以看到两个按钮)当点击按钮,应该调用本地(html嵌入)javascript它的工作。点击第二个按钮没有任何作用。
似乎外部assets.js没有正确加载。有什么建议吗?
谢谢。
Javascript不会从html中求值。找不到你的JS文件。以下内容无效:
<script src="assets.js"></script>
显式地试试:
const QString js = readFile("qrc:/web/assets.js"); // Load your javascript file
view->page()->mainFrame()->evaluateJavaScript(js);
另一个更有效的解决方案:使用QRC系统并在sethhtml中设置baseUrl以正确链接所有文件:
const QString html = readFile(":/index.html");
view->setHtml(html, QUrl("qrc:/"));
相关文章:
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何在运行中期切换GTK CSS style_context
- 如何在MS Visual Studio 2019中运行QT UI
- glfw 的基本设置会导致与 emscripten 生成的 js 文件中的事件侦听器有关的运行时错误
- 以有效的方式从 Node .js运行 C 或C++代码
- 在 Node.JS 中运行.EXE
- 如何在 v8 的 none 主线程中运行 js?
- libuv(node.js的异步库)可以在Apple IOS / Android上运行吗?
- QWebView可以从Qt资源文件中加载*.js并运行它们吗?
- 在V8中异步运行c++和JS代码
- 从Node.js运行C库
- 节点.js本机插件 - 包装类的析构函数不运行
- 为什么asm.js项目在Chrome中的运行速度比FireFox快
- node.js/[我自己的库/插件]如何在v8上运行?