Exemplo n.º 1
0
 def setUp(self):
     UsesQApplication.setUp(self)
     self.engine = QScriptEngine()
     self.debugger = QScriptEngineDebugger()
     self.has_suspended = 0
     self.has_resumed = 0
     self.count = 3
Exemplo n.º 2
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    app = QApplication(argv)
    engine = QScriptEngine()

    if HAS_DEBUGGER:
        debugger = QScriptEngineDebugger()
        debugger.attachTo(engine)
        debugWindow = debugger.standardWindow()
        debugWindow.resize(1024, 640)

    scriptFileName = './calculator.js'
    scriptFile = QFile(scriptFileName)
    scriptFile.open(QIODevice.ReadOnly)
    engine.evaluate(unicode(scriptFile.readAll()), scriptFileName)
    scriptFile.close()

    loader = QUiLoader()
    ui = loader.load(':/calculator.ui')

    ctor = engine.evaluate('Calculator')
    scriptUi = engine.newQObject(ui, QScriptEngine.ScriptOwnership)
    calc = ctor.construct([scriptUi])

    if HAS_DEBUGGER:
        display = ui.findChild(QLineEdit, 'display')
        display.connect(display, SIGNAL('returnPressed()'), debugWindow,
                        SLOT('show()'))

    ui.show()
    return app.exec_()
Exemplo n.º 3
0
 def testQScriptEngine(self):
     engine = QScriptEngine()
     obj = engine.evaluate(
         "({ unitName: 'Celsius', toKelvin: function(x) { return x + 273; } })"
     )
     toKelvin = obj.property("toKelvin")
     result = toKelvin.call(obj, [100])
     self.assertEqual(result.toNumber(), 373)
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
    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);
}
    """)
Exemplo n.º 6
0
 def engine(self):
     if not self._engine:
         self._engine = QScriptEngine()
     return self._engine