How to correctly return values ​​from pyqt in JavaScript?

I already found and edited the answer below.

I want to return values ​​from python code to javascript context in QtWebKit. So far I have this class:

class Extensions(QtCore.QObject): @QtCore.pyqtSlot() def constant_one(self): return 1; # ... later, in code e = Extensions(); def addextensions(): webview.page().mainFrame().addToJavaScriptWindowObject("extensions", e); # ... webview.connect(webview.page().mainFrame(), QtCore.SIGNAL("javaScriptWindowObjectCleared"), addextensions) 

I can call this function from Javascript as follows:

 var a = extensions.constant_one(); 

and it really is called (I checked with print there); but still ends up undefined. Why doesn't it get the value returned by the function? I also tried wrapping a in QVariant, but still no dice.

Edit: I found the answer. Obviously, QtWebKit needs a result type as a hint. You can provide this with pyqtSlot-Decorator, for example:

 class Extensions(QtCore.QObject): @QtCore.pyqtSlot(result="int") def constant_one(self): return 1; 

and then it works correctly. Leaving it open for another two days if someone finds something else that I should do.

+8
qt pyqt qtwebkit
source share
1 answer

shortly after posting the question, I myself found the answer: apparently, QtWebKit needs a result type as a hint. You can provide this with pyqtSlot-Decorator, for example:

 class Extensions(QtCore.QObject): @QtCore.pyqtSlot(result="int") # just int (type object) also works def constant_one(self): return 1; 

and then it works correctly.

+9
source share

All Articles