C++ QWebView可以从Qt资源文件加载*.js并运行它们吗?
我有qrc文件,看起来像这样:C++ QWebView可以从Qt资源文件加载*.js并运行它们吗?,c++,windows,qt,qtwebkit,qtwebview,C++,Windows,Qt,Qtwebkit,Qtwebview,我有qrc文件,看起来像这样: <qresource prefix="/web"> <file alias="assets.js">../web/assets.js</file> <file alias="index.html">../web/index.html</file> </qresource> 在index.html内部,为警报弹出窗口添加另一个javascript,加载asse
<qresource prefix="/web">
<file alias="assets.js">../web/assets.js</file>
<file alias="index.html">../web/index.html</file>
</qresource>
在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已加载正常(我可以看到2个按钮)
当点击应该调用本地(嵌入html的)javascript的按钮时,它会工作。
单击第二个按钮不起任何作用
外部资产.js似乎没有正确加载。
有什么建议我怎样才能让它工作
谢谢。Javascript不是从html中计算出来的。找不到您的JS文件。以下内容无效:
<script src="assets.js"></script>
另一种更有效的解决方案:使用QRC系统并在setHtml中设置baseUrl以正确链接所有文件:
const QString html = readFile(":/index.html");
view->setHtml(html, QUrl("qrc:/"));
这个问题的答案可能会有所帮助。
<script src="assets.js"></script>
const QString js = readFile("qrc:/web/assets.js"); // Load your javascript file
view->page()->mainFrame()->evaluateJavaScript(js);
const QString html = readFile(":/index.html");
view->setHtml(html, QUrl("qrc:/"));