source: trunk/doc/src/snippets/code/src_script_qscriptengine.cpp@ 5

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

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

File size: 6.4 KB
Line 
1//! [0]
2QScriptEngine myEngine;
3QScriptValue three = myEngine.evaluate("1 + 2");
4//! [0]
5
6
7//! [1]
8QScriptValue fun = myEngine.evaluate("function(a, b) { return a + b; }");
9QScriptValueList args;
10args << 1 << 2;
11QScriptValue threeAgain = fun.call(QScriptValue(), args);
12//! [1]
13
14
15//! [2]
16QString fileName = "helloworld.qs";
17QFile scriptFile(fileName);
18if (!scriptFile.open(QIODevice::ReadOnly))
19 // handle error
20QTextStream stream(&scriptFile);
21QString contents = stream.readAll();
22scriptFile.close();
23myEngine.evaluate(contents, fileName);
24//! [2]
25
26
27//! [3]
28myEngine.globalObject().setProperty("myNumber", 123);
29...
30QScriptValue myNumberPlusOne = myEngine.evaluate("myNumber + 1");
31//! [3]
32
33
34//! [4]
35QScriptValue result = myEngine.evaluate(...);
36if (myEngine.hasUncaughtException()) {
37 int line = myEngine.uncaughtExceptionLineNumber();
38 qDebug() << "uncaught exception at line" << line << ":" << result.toString();
39}
40//! [4]
41
42
43//! [5]
44QPushButton button;
45QScriptValue scriptButton = myEngine.newQObject(&button);
46myEngine.globalObject().setProperty("button", scriptButton);
47
48myEngine.evaluate("button.checkable = true");
49
50qDebug() << scriptButton.property("checkable").toBoolean();
51scriptButton.property("show").call(); // call the show() slot
52//! [5]
53
54
55//! [6]
56QScriptValue myAdd(QScriptContext *context, QScriptEngine *engine)
57{
58 QScriptValue a = context->argument(0);
59 QScriptValue b = context->argument(1);
60 return a.toNumber() + b.toNumber();
61}
62//! [6]
63
64
65//! [7]
66QScriptValue fun = myEngine.newFunction(myAdd);
67myEngine.globalObject().setProperty("myAdd", fun);
68//! [7]
69
70
71//! [8]
72QScriptValue result = myEngine.evaluate("myAdd(myNumber, 1)");
73//! [8]
74
75
76//! [9]
77QScriptValue Foo(QScriptContext *context, QScriptEngine *engine)
78{
79 if (context->calledAsConstructor()) {
80 // initialize the new object
81 context->thisObject().setProperty("bar", ...);
82 // ...
83 // return a non-object value to indicate that the
84 // thisObject() should be the result of the "new Foo()" expression
85 return engine->undefinedValue();
86 } else {
87 // not called as "new Foo()", just "Foo()"
88 // create our own object and return that one
89 QScriptValue object = engine->newObject();
90 object.setPrototype(context->callee().property("prototype"));
91 object.setProperty("baz", ...);
92 return object;
93 }
94}
95
96...
97
98QScriptValue fooProto = engine->newObject();
99fooProto.setProperty("whatever", ...);
100engine->globalObject().setProperty("Foo", engine->newFunction(Foo, fooProto));
101//! [9]
102
103
104//! [10]
105class Bar { ... };
106
107Q_DECLARE_METATYPE(Bar)
108
109QScriptValue constructBar(QScriptContext *context, QScriptEngine *engine)
110{
111 Bar bar;
112 // initialize from arguments in context, if desired
113 ...
114 return engine->toScriptValue(bar);
115}
116
117class BarPrototype : public QObject, public QScriptable
118{
119// provide the scriptable interface of this type using slots and properties
120...
121};
122
123...
124
125// create and register the Bar prototype and constructor in the engine
126BarPrototype *barPrototypeObject = new BarPrototype(...);
127QScriptValue barProto = engine->newQObject(barPrototypeObject);
128engine->setDefaultPrototype(qMetaTypeId<Bar>, barProto);
129QScriptValue barCtor = engine->newFunction(constructBar, barProto);
130engine->globalObject().setProperty("Bar", barCtor);
131//! [10]
132
133
134//! [11]
135static QScriptValue getSetFoo(QScriptContext *context, QScriptEngine *engine)
136{
137 QScriptValue callee = context->callee();
138 if (context->argumentCount() == 1) // writing?
139 callee.setProperty("value", context->argument(0));
140 return callee.property("value");
141}
142
143....
144
145QScriptValue object = engine.newObject();
146object.setProperty("foo", engine.newFunction(getSetFoo),
147 QScriptValue::PropertyGetter | QScriptValue::PropertySetter);
148//! [11]
149
150
151//! [12]
152QScriptValue object = engine.newObject();
153object.setProperty("foo", engine.newFunction(getFoo), QScriptValue::PropertyGetter);
154object.setProperty("foo", engine.newFunction(setFoo), QScriptValue::PropertySetter);
155//! [12]
156
157
158//! [13]
159Q_SCRIPT_DECLARE_QMETAOBJECT(QLineEdit, QWidget*)
160
161...
162
163QScriptValue lineEditClass = engine.scriptValueFromQMetaObject<QLineEdit>();
164engine.globalObject().setProperty("QLineEdit", lineEditClass);
165//! [13]
166
167
168//! [14]
169if (hello && world)
170 print("hello world");
171//! [14]
172
173
174//! [15]
175if (hello &&
176//! [15]
177
178
179//! [16]
1800 = 0
181//! [16]
182
183
184//! [17]
185./test.js
186//! [17]
187
188
189//! [18]
190foo["bar"]
191//! [18]
192
193
194//! [19]
195QScriptEngine engine;
196QScriptContext *context = engine.pushContext();
197context->activationObject().setProperty("myArg", 123);
198engine.evaluate("var tmp = myArg + 42");
199...
200engine.popContext();
201//! [19]
202
203
204//! [20]
205struct MyStruct {
206 int x;
207 int y;
208};
209//! [20]
210
211
212//! [21]
213Q_DECLARE_METATYPE(MyStruct)
214//! [21]
215
216
217//! [22]
218QScriptValue toScriptValue(QScriptEngine *engine, const MyStruct &s)
219{
220 QScriptValue obj = engine->newObject();
221 obj.setProperty("x", s.x);
222 obj.setProperty("y", s.y);
223 return obj;
224}
225
226void fromScriptValue(const QScriptValue &obj, MyStruct &s)
227{
228 s.x = obj.property("x").toInt32();
229 s.y = obj.property("y").toInt32();
230}
231//! [22]
232
233
234//! [23]
235qScriptRegisterMetaType(engine, toScriptValue, fromScriptValue);
236//! [23]
237
238
239//! [24]
240MyStruct s = qscriptvalue_cast<MyStruct>(context->argument(0));
241...
242MyStruct s2;
243s2.x = s.x + 10;
244s2.y = s.y + 20;
245QScriptValue v = engine->toScriptValue(s2);
246//! [24]
247
248
249//! [25]
250QScriptValue createMyStruct(QScriptContext *, QScriptEngine *engine)
251{
252 MyStruct s;
253 s.x = 123;
254 s.y = 456;
255 return engine->toScriptValue(s);
256}
257...
258QScriptValue ctor = engine.newFunction(createMyStruct);
259engine.globalObject().setProperty("MyStruct", ctor);
260//! [25]
261
262
263//! [26]
264Q_DECLARE_METATYPE(QVector<int>)
265
266...
267
268qScriptRegisterSequenceMetaType<QVector<int> >(engine);
269...
270QVector<int> v = qscriptvalue_cast<QVector<int> >(engine->evaluate("[5, 1, 3, 2]"));
271qSort(v.begin(), v.end());
272QScriptValue a = engine->toScriptValue(v);
273qDebug() << a.toString(); // outputs "[1, 2, 3, 5]"
274//! [26]
275
276//! [27]
277QScriptValue mySpecialQObjectConstructor(QScriptContext *context,
278 QScriptEngine *engine)
279{
280 QObject *parent = context->argument(0).toQObject();
281 QObject *object = new QObject(parent);
282 return engine->newQObject(object, QScriptEngine::ScriptOwnership);
283}
284
285...
286
287QScriptValue ctor = engine.newFunction(mySpecialQObjectConstructor);
288QScriptValue metaObject = engine.newQMetaObject(&QObject::staticMetaObject, ctor);
289engine.globalObject().setProperty("QObject", metaObject);
290
291QScriptValue result = engine.evaluate("new QObject()");
292//! [27]
Note: See TracBrowser for help on using the repository browser.