int main(int argc, char *argv[]) { QApplication app(argc, argv); QDeclarativeView view; QDeclarativeContext *context = view.rootContext(); context->setContextProperty("backgroundColor", QColor(Qt::yellow)); KDeclarative kdeclarative; kdeclarative.setDeclarativeEngine(view.engine()); kdeclarative.initialize(); //binds things like kconfig and icons kdeclarative.setupBindings(); //If all gone well, the QScriptEngine has been extracted QScriptEngine *scriptEngine = kdeclarative.scriptEngine(); Q_ASSERT(scriptEngine); //Bind a test QObject in the "QtScript way" QScriptValue global = scriptEngine->globalObject(); TestObject *testObject = new TestObject(); QScriptValue testValue = scriptEngine->newQObject(testObject); testValue.setScope(global); global.setProperty("testObject", testValue); view.setSource(QUrl::fromLocalFile("test.qml")); view.show(); return app.exec(); }
//! [72] QScriptValue counter(QScriptContext *ctx, QScriptEngine *eng) { QScriptValue act = ctx->activationObject(); act.setProperty("count", 0); QScriptValue result = eng->newFunction(counter_inner); result.setScope(act); return result; }