def testScriptQProperty(self): qapp = QCoreApplication([]) myEngine = QScriptEngine() obj = MyObject() scriptObj = myEngine.newQObject(obj) myEngine.globalObject().setProperty("obj", scriptObj) myEngine.evaluate("obj.x = 42") self.assertEqual(scriptObj.property("x").toInt32(), 42) self.assertEqual(obj.property("x"), 42)
class JsEngine: def __init__(self): self.app = QApplication.instance() if self.app is None: self.app = QCoreApplication([]) self.engine = QScriptEngine() self.globalObject = self.engine.globalObject() # There were some problems evalating javascript inside a function callback. QtSide's bindings for QScriptEngine # didn't seem prepared to handle it (it breaks in weird hard-to-debug ways) # It is however not a problem if you use a wrapped QObject for the callback instead of using engine.newFunction(). # The workaround here is to pass a wrapped QObject, and then evaluate javascript to create a function that calls # a method of the wrapped QObject. # Also note: Wrapped QObjects passed to QtSide are not refcounted in Python! To work around this, a reference to # "RequireObj" is stored in the JSEngine instance self.requireObj = JsEngineRequireClass(self.engine) self.addObject('RequireObj', self.requireObj) self.evaluate(""" function require(arg) { return RequireObj.require(arg); } """) # Sets a variable in the global object def addObject(self, name, obj): wrappedObject = pyObjectToScriptValue(self.engine, obj) self.engine.globalObject().setProperty(name, wrappedObject) # Adds a python function to the global object, wrapping it def addFunction(self, name, func): f = lambda context, engine: pyObjectToScriptValue( engine, func(*_contextToArguments(context))) wrappedFunction = self.engine.newFunction(f) self.engine.globalObject().setProperty(name, wrappedFunction) # Adds a python function to the global object, not wrapping it (it gets passed raw context and engine arguments) def addRawFunction(self, name, func): wrappedFunction = self.engine.newFunction(func) self.engine.globalObject().setProperty(name, wrappedFunction) # Evaluate some javascript code def evaluate(self, code): result = self.engine.evaluate(code) if self.engine.hasUncaughtException(): raise JsEngineException(result, self.engine) return scriptValueToPyObject(result)
class JsEngine: def __init__(self): self.app = QApplication.instance() if self.app is None: self.app = QCoreApplication([]) self.engine = QScriptEngine() self.globalObject = self.engine.globalObject() # There were some problems evalating javascript inside a function callback. QtSide's bindings for QScriptEngine # didn't seem prepared to handle it (it breaks in weird hard-to-debug ways) # It is however not a problem if you use a wrapped QObject for the callback instead of using engine.newFunction(). # The workaround here is to pass a wrapped QObject, and then evaluate javascript to create a function that calls # a method of the wrapped QObject. # Also note: Wrapped QObjects passed to QtSide are not refcounted in Python! To work around this, a reference to # "RequireObj" is stored in the JSEngine instance self.requireObj = JsEngineRequireClass(self.engine) self.addObject('RequireObj', self.requireObj) self.evaluate(""" function require(arg) { return RequireObj.require(arg); } """) # Sets a variable in the global object def addObject(self, name, obj): wrappedObject = pyObjectToScriptValue(self.engine, obj) self.engine.globalObject().setProperty(name, wrappedObject) # Adds a python function to the global object, wrapping it def addFunction(self, name, func): f = lambda context,engine: pyObjectToScriptValue(engine, func(*_contextToArguments(context))) wrappedFunction = self.engine.newFunction(f) self.engine.globalObject().setProperty(name, wrappedFunction) # Adds a python function to the global object, not wrapping it (it gets passed raw context and engine arguments) def addRawFunction(self, name, func): wrappedFunction = self.engine.newFunction(func) self.engine.globalObject().setProperty(name, wrappedFunction) # Evaluate some javascript code def evaluate(self, code): result = self.engine.evaluate(code) if self.engine.hasUncaughtException(): raise JsEngineException(result, self.engine) return scriptValueToPyObject(result)