QWebView not loading external javascript?

天涯浪子 提交于 2019-12-01 12:02:39

Alternatively 1:

It seems the javascript isn't evaluated from the html. In other words, the following has no effect:

<script type="text/javascript" src="qt.js">
</script>

It must be done explicitly:

QString js = readFile("qt.js");
view->page()->mainFrame()->evaluateJavaScript(js);

And, there is no need to set baseUrl in setHtml().

Alternatively 2:

Use the QRC System and set the baseUrl in setHtml. This way doesn't require a call to view->page()->mainFrame()->evaluateJavaScript();

//    QString js = readFile(":/qt.js");
//    view->page()->mainFrame()->evaluateJavaScript(js);

    QString html = readFile(":/qt.html");
    view->setHtml(html, QUrl("qrc:/"));

application.qrc

<!DOCTYPE RCC><RCC version="1.0">
<qresource>
    <file alias="qt.png">resource/qt.png</file>
    <file alias="image.html">resource/image.html</file>
    <file alias="qt.html">resource/qt.html</file>
    <file alias="qt.js">resource/qt.js</file>
</qresource>
</RCC>

Actually you can. You just need to add file:// at the begining of your src.

file:///home/js/somejs.js

If you want to load a local html file, first you need to change the src attribute of the javascript, for example:

then src is changed to src="'file:///D:/home/myWeb/js/myjs.js'"

this will work. Relative path can also be given.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!