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

Last change on this file since 728 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 8.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42//! [0]
43QScriptEngine myEngine;
44QScriptValue three = myEngine.evaluate("1 + 2");
45//! [0]
46
47
48//! [1]
49QScriptValue fun = myEngine.evaluate("(function(a, b) { return a + b; })");
50QScriptValueList args;
51args << 1 << 2;
52QScriptValue threeAgain = fun.call(QScriptValue(), args);
53//! [1]
54
55
56//! [2]
57QString fileName = "helloworld.qs";
58QFile scriptFile(fileName);
59if (!scriptFile.open(QIODevice::ReadOnly))
60 // handle error
61QTextStream stream(&scriptFile);
62QString contents = stream.readAll();
63scriptFile.close();
64myEngine.evaluate(contents, fileName);
65//! [2]
66
67
68//! [3]
69myEngine.globalObject().setProperty("myNumber", 123);
70...
71QScriptValue myNumberPlusOne = myEngine.evaluate("myNumber + 1");
72//! [3]
73
74
75//! [4]
76QScriptValue result = myEngine.evaluate(...);
77if (myEngine.hasUncaughtException()) {
78 int line = myEngine.uncaughtExceptionLineNumber();
79 qDebug() << "uncaught exception at line" << line << ":" << result.toString();
80}
81//! [4]
82
83
84//! [5]
85QPushButton button;
86QScriptValue scriptButton = myEngine.newQObject(&button);
87myEngine.globalObject().setProperty("button", scriptButton);
88
89myEngine.evaluate("button.checkable = true");
90
91qDebug() << scriptButton.property("checkable").toBoolean();
92scriptButton.property("show").call(); // call the show() slot
93//! [5]
94
95
96//! [6]
97QScriptValue myAdd(QScriptContext *context, QScriptEngine *engine)
98{
99 QScriptValue a = context->argument(0);
100 QScriptValue b = context->argument(1);
101 return a.toNumber() + b.toNumber();
102}
103//! [6]
104
105
106//! [7]
107QScriptValue fun = myEngine.newFunction(myAdd);
108myEngine.globalObject().setProperty("myAdd", fun);
109//! [7]
110
111
112//! [8]
113QScriptValue result = myEngine.evaluate("myAdd(myNumber, 1)");
114//! [8]
115
116
117//! [9]
118QScriptValue Foo(QScriptContext *context, QScriptEngine *engine)
119{
120 if (context->calledAsConstructor()) {
121 // initialize the new object
122 context->thisObject().setProperty("bar", ...);
123 // ...
124 // return a non-object value to indicate that the
125 // thisObject() should be the result of the "new Foo()" expression
126 return engine->undefinedValue();
127 } else {
128 // not called as "new Foo()", just "Foo()"
129 // create our own object and return that one
130 QScriptValue object = engine->newObject();
131 object.setPrototype(context->callee().property("prototype"));
132 object.setProperty("baz", ...);
133 return object;
134 }
135}
136
137...
138
139QScriptValue fooProto = engine->newObject();
140fooProto.setProperty("whatever", ...);
141engine->globalObject().setProperty("Foo", engine->newFunction(Foo, fooProto));
142//! [9]
143
144
145//! [10]
146class Bar { ... };
147
148Q_DECLARE_METATYPE(Bar)
149
150QScriptValue constructBar(QScriptContext *context, QScriptEngine *engine)
151{
152 Bar bar;
153 // initialize from arguments in context, if desired
154 ...
155 return engine->toScriptValue(bar);
156}
157
158class BarPrototype : public QObject, public QScriptable
159{
160// provide the scriptable interface of this type using slots and properties
161...
162};
163
164...
165
166// create and register the Bar prototype and constructor in the engine
167BarPrototype *barPrototypeObject = new BarPrototype(...);
168QScriptValue barProto = engine->newQObject(barPrototypeObject);
169engine->setDefaultPrototype(qMetaTypeId<Bar>, barProto);
170QScriptValue barCtor = engine->newFunction(constructBar, barProto);
171engine->globalObject().setProperty("Bar", barCtor);
172//! [10]
173
174
175//! [11]
176static QScriptValue getSetFoo(QScriptContext *context, QScriptEngine *engine)
177{
178 QScriptValue callee = context->callee();
179 if (context->argumentCount() == 1) // writing?
180 callee.setProperty("value", context->argument(0));
181 return callee.property("value");
182}
183
184....
185
186QScriptValue object = engine.newObject();
187object.setProperty("foo", engine.newFunction(getSetFoo),
188 QScriptValue::PropertyGetter | QScriptValue::PropertySetter);
189//! [11]
190
191
192//! [12]
193QScriptValue object = engine.newObject();
194object.setProperty("foo", engine.newFunction(getFoo), QScriptValue::PropertyGetter);
195object.setProperty("foo", engine.newFunction(setFoo), QScriptValue::PropertySetter);
196//! [12]
197
198
199//! [13]
200Q_SCRIPT_DECLARE_QMETAOBJECT(QLineEdit, QWidget*)
201
202...
203
204QScriptValue lineEditClass = engine.scriptValueFromQMetaObject<QLineEdit>();
205engine.globalObject().setProperty("QLineEdit", lineEditClass);
206//! [13]
207
208
209//! [14]
210if (hello && world)
211 print("hello world");
212//! [14]
213
214
215//! [15]
216if (hello &&
217//! [15]
218
219
220//! [16]
2210 = 0
222//! [16]
223
224
225//! [17]
226./test.js
227//! [17]
228
229
230//! [18]
231foo["bar"]
232//! [18]
233
234
235//! [19]
236QScriptEngine engine;
237QScriptContext *context = engine.pushContext();
238context->activationObject().setProperty("myArg", 123);
239engine.evaluate("var tmp = myArg + 42");
240...
241engine.popContext();
242//! [19]
243
244
245//! [20]
246struct MyStruct {
247 int x;
248 int y;
249};
250//! [20]
251
252
253//! [21]
254Q_DECLARE_METATYPE(MyStruct)
255//! [21]
256
257
258//! [22]
259QScriptValue toScriptValue(QScriptEngine *engine, const MyStruct &s)
260{
261 QScriptValue obj = engine->newObject();
262 obj.setProperty("x", s.x);
263 obj.setProperty("y", s.y);
264 return obj;
265}
266
267void fromScriptValue(const QScriptValue &obj, MyStruct &s)
268{
269 s.x = obj.property("x").toInt32();
270 s.y = obj.property("y").toInt32();
271}
272//! [22]
273
274
275//! [23]
276qScriptRegisterMetaType(engine, toScriptValue, fromScriptValue);
277//! [23]
278
279
280//! [24]
281MyStruct s = qscriptvalue_cast<MyStruct>(context->argument(0));
282...
283MyStruct s2;
284s2.x = s.x + 10;
285s2.y = s.y + 20;
286QScriptValue v = engine->toScriptValue(s2);
287//! [24]
288
289
290//! [25]
291QScriptValue createMyStruct(QScriptContext *, QScriptEngine *engine)
292{
293 MyStruct s;
294 s.x = 123;
295 s.y = 456;
296 return engine->toScriptValue(s);
297}
298...
299QScriptValue ctor = engine.newFunction(createMyStruct);
300engine.globalObject().setProperty("MyStruct", ctor);
301//! [25]
302
303
304//! [26]
305Q_DECLARE_METATYPE(QVector<int>)
306
307...
308
309qScriptRegisterSequenceMetaType<QVector<int> >(engine);
310...
311QVector<int> v = qscriptvalue_cast<QVector<int> >(engine->evaluate("[5, 1, 3, 2]"));
312qSort(v.begin(), v.end());
313QScriptValue a = engine->toScriptValue(v);
314qDebug() << a.toString(); // outputs "[1, 2, 3, 5]"
315//! [26]
316
317//! [27]
318QScriptValue mySpecialQObjectConstructor(QScriptContext *context,
319 QScriptEngine *engine)
320{
321 QObject *parent = context->argument(0).toQObject();
322 QObject *object = new QObject(parent);
323 return engine->newQObject(object, QScriptEngine::ScriptOwnership);
324}
325
326...
327
328QScriptValue ctor = engine.newFunction(mySpecialQObjectConstructor);
329QScriptValue metaObject = engine.newQMetaObject(&QObject::staticMetaObject, ctor);
330engine.globalObject().setProperty("QObject", metaObject);
331
332QScriptValue result = engine.evaluate("new QObject()");
333//! [27]
Note: See TracBrowser for help on using the repository browser.