PyQt QtWebKit - Return value from a Python method directly to JavaScript -
edit:
i've found solution here: how correctly return values pyqt javascript?
i'll post code first:
python code:
class jsbridge(qobject): def __init__(self, parent=none): super(jsbridge, self).__init__(parent) @pyqtslot(bool) def fromjstopython(self, param): print param print param.tobool() @pyqtslot() def returnvalue() return "hello world" class another(): ... view = qwebview() frame = view.page().mainframe() param = "blabla" frame.evaluatejavascript("printit('" + param + "');") parambool = true frame.evaluatejavascript("frompythonwithparameterbool('" + parambool + "');")
javascript:
function printit(param) { alert(param); } function topython() { jsbridgeinst.fromjstopython(true); } // here functions i've questions about: function frompythonwithparameterbool(param) { alert(param); } function frompythonreturnvalue() { res = jsbridgeinst.returnvalue(); alert(res); }
now question:
the printit
function works fine. param interpreted string. fromjstopython
function works fine. print statement shows it's qvariant
.
but frompythonwithparameterbool
function not work, because have convert parambool
string in order connect it. if so, it's printing string value (but want boolean). possible pass boolean, in javascript can work boolean? if yes, how do it?
and frompythonreturnvalue
function, not show error, res
undefined. why? searching problem, examples/tutorials show standard stuff like:
evaluatejavascript("alert('9')";)
i've worked java , swt, , there return value (as in formpythonreturnvalue
method). there browser class implement methods call, jsbridge.
the frompythonwithparameterbool
doesn't work because not generating input string can evaluated javascript.
what need this:
>>> script = 'frompythonwithparameterbool(%s);' % str(bool(parambool)).lower() >>> print(script) frompythonwithparameterbool(true);
so resulting string fragment of valid javascript.
the problem frompythonreturnvalue
(as seem have discovered), not specifying type of return value. has done explicitly, using pyqtslot decorator:
@pyqtslot(result=str) def returnvalue(self): return 'hello world'
Comments
Post a Comment