QWebView not loading external javascript?

后端 未结 3 946
失恋的感觉
失恋的感觉 2021-01-16 05:51

It is possible to load an external javascript file from the html using QWebView?

In the following QtProject (all files in the same directory) there is javascript cod

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-16 06:21

    Alternatively 1:

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

    
    

    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

    
    
        resource/qt.png
        resource/image.html
        resource/qt.html
        resource/qt.js
    
    
    

提交回复
热议问题