source: trunk/doc/src/snippets/code/src_script_qscriptvalue.cpp@ 244

Last change on this file since 244 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 1.1 KB
Line 
1//! [0]
2QScriptEngine myEngine;
3QScriptValue myObject = myEngine.newObject();
4QScriptValue myOtherObject = myEngine.newObject();
5myObject.setProperty("myChild", myOtherObject);
6myObject.setProperty("name", "John Doe");
7//! [0]
8
9
10//! [1]
11QScriptValue val(&myEngine, 123);
12myObject.setProperty("myReadOnlyProperty", val, QScriptValue::ReadOnly);
13//! [1]
14
15
16//! [2]
17QScriptEngine engine;
18engine.evaluate("function fullName() { return this.firstName + ' ' + this.lastName; }");
19engine.evaluate("somePerson = { firstName: 'John', lastName: 'Doe' }");
20
21QScriptValue global = engine.globalObject();
22QScriptValue fullName = global.property("fullName");
23QScriptValue who = global.property("somePerson");
24qDebug() << fullName.call(who).toString(); // "John Doe"
25
26engine.evaluate("function cube(x) { return x * x * x; }");
27QScriptValue cube = global.property("cube");
28QScriptValueList args;
29args << 3;
30qDebug() << cube.call(QScriptValue(), args).toNumber(); // 27
31//! [2]
32
33
34//! [3]
35QScriptValue myNativeFunction(QScriptContext *ctx, QScriptEngine *)
36{
37 QScriptValue otherFunction = ...;
38 return otherFunction.call(ctx->thisObject(), ctx->argumentsObject());
39}
40//! [3]
Note: See TracBrowser for help on using the repository browser.