Important: Please read the Qt Code of Conduct -

QGraphicsWebView injecting script does not work

  • I'm trying to inject a bit of javascript to handle the window.onload event, and I can't seem to get it to work.

    I connect to the loadFinished signal, and get the <head> tag, to which I try to add javascript code using the following slot function:

    void Html5ApplicationViewerDerived::InjectJavascript()
    QWebElement head = webView()->page()->mainFrame()->findFirstElement("head");
    QString scriptText = "<script>window.onload = function() { alert('Foo!'); }</script>";

    When I inspect the document, the new <script> tag does not appear at all. I am able to append to the body element with other types of tags, such as <label>, but I cannot seem to add to the <head> element. If I get a script element inside <head>, I can append to that, but the new script does not execute. I suspect that the loadFinished signal is emitted after the window.onload event happens. There is no guarantee that any script element will exist in any particular html file that I load, so I need to be able to create one.

  • Update:

    after some more testing, I found that QGraphicsWebView's loadFinished signal does not get sent until after window.onload has completed.

    QWebFrame's initialLayoutCompleted signal gets sent after window.onload, but seems to run concurrently with it, not waiting for it to finish.

Log in to reply