Important: Please read the Qt Code of Conduct -

Bindings from Javascript to Webkit

  • Hi,

    i'm using PyQt to display a HTML page in QtWebkit. Now i wan't to expose an Object to Javascript with "addToJavaScriptWindowObject". Everyting works until i wan't to callback from Python to Javascript with a callback! For Example

    @class X (QtObject):
    def dosomething (self, onok):
    # do some some task....
    # the following does NOT work, but that's what i want :-)
    onok.ready ()@

    now i can do this:"myX", X())@

    and in javascriptcode:

    @ ...
    myX.dosomething (function () {
    alert("no ready");

    as you can see, the parameter for my hostobject "myX" is a javascript function. i have not found a way to access this in python (or point me to some c++ documentation, would be ok too). the seconed parameter in "dosomething" is annotated as a "QWebElement" as i thought it could be a webelement. what type is this parameter? if i give the "wrong" type the whole "dosomething" function is not called.

    how can i call from python back to javascript?

    btw: i tried to use pyside, but it crashes completely, so i use pyqt.

    thank you


  • I don't think it is possible at all with the current bridge (look at qt_runtime.cpp). That should be possible in the future when QtScript and QtWebKit will be integrated, but with the current bridge, I don't think you can do that.

    The closest workaround I can think of is to pass a string with the name of a callback function, and invoke the function with QWebFrame::evaluateJavaScript().

  • Hi Benjamin, I'm having the same need... I can do this already with PyQt5? or with cpp?

    Thank you!

Log in to reply