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

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 8.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41//! [0]
42QScriptEngine myEngine;
43QScriptValue three = myEngine.evaluate("1 + 2");
44//! [0]
45
46
47//! [1]
48QScriptValue fun = myEngine.evaluate("(function(a, b) { return a + b; })");
49QScriptValueList args;
50args << 1 << 2;
51QScriptValue threeAgain = fun.call(QScriptValue(), args);
52//! [1]
53
54
55//! [2]
56QString fileName = "helloworld.qs";
57QFile scriptFile(fileName);
58if (!scriptFile.open(QIODevice::ReadOnly))
59 // handle error
60QTextStream stream(&scriptFile);
61QString contents = stream.readAll();
62scriptFile.close();
63myEngine.evaluate(contents, fileName);
64//! [2]
65
66
67//! [3]
68myEngine.globalObject().setProperty("myNumber", 123);
69...
70QScriptValue myNumberPlusOne = myEngine.evaluate("myNumber + 1");
71//! [3]
72
73
74//! [4]
75QScriptValue result = myEngine.evaluate(...);
76if (myEngine.hasUncaughtException()) {
77 int line = myEngine.uncaughtExceptionLineNumber();
78 qDebug() << "uncaught exception at line" << line << ":" << result.toString();
79}
80//! [4]
81
82
83//! [5]
84QPushButton button;
85QScriptValue scriptButton = myEngine.newQObject(&button);
86myEngine.globalObject().setProperty("button", scriptButton);
87
88myEngine.evaluate("button.checkable = true");
89
90qDebug() << scriptButton.property("checkable").toBoolean();
91scriptButton.property("show").call(); // call the show() slot
92//! [5]
93
94
95//! [6]
96QScriptValue myAdd(QScriptContext *context, QScriptEngine *engine)
97{
98 QScriptValue a = context->argument(0);
99 QScriptValue b = context->argument(1);
100 return a.toNumber() + b.toNumber();
101}
102//! [6]
103
104
105//! [7]
106QScriptValue fun = myEngine.newFunction(myAdd);
107myEngine.globalObject().setProperty("myAdd", fun);
108//! [7]
109
110
111//! [8]
112QScriptValue result = myEngine.evaluate("myAdd(myNumber, 1)");
113//! [8]
114
115
116//! [9]
117QScriptValue Foo(QScriptContext *context, QScriptEngine *engine)
118{
119 if (context->calledAsConstructor()) {
120 // initialize the new object
121 context->thisObject().setProperty("bar", ...);
122 // ...
123 // return a non-object value to indicate that the
124 // thisObject() should be the result of the "new Foo()" expression
125 return engine->undefinedValue();
126 } else {
127 // not called as "new Foo()", just "Foo()"
128 // create our own object and return that one
129 QScriptValue object = engine->newObject();
130 object.setPrototype(context->callee().property("prototype"));
131 object.setProperty("baz", ...);
132 return object;
133 }
134}
135
136...
137
138QScriptValue fooProto = engine->newObject();
139fooProto.setProperty("whatever", ...);
140engine->globalObject().setProperty("Foo", engine->newFunction(Foo, fooProto));
141//! [9]
142
143
144//! [10]
145class Bar { ... };
146
147Q_DECLARE_METATYPE(Bar)
148
149QScriptValue constructBar(QScriptContext *context, QScriptEngine *engine)
150{
151 Bar bar;
152 // initialize from arguments in context, if desired
153 ...
154 return engine->toScriptValue(bar);
155}
156
157class BarPrototype : public QObject, public QScriptable
158{
159// provide the scriptable interface of this type using slots and properties
160...
161};
162
163...
164
165// create and register the Bar prototype and constructor in the engine
166BarPrototype *barPrototypeObject = new BarPrototype(...);
167QScriptValue barProto = engine->newQObject(barPrototypeObject);
168engine->setDefaultPrototype(qMetaTypeId<Bar>, barProto);
169QScriptValue barCtor = engine->newFunction(constructBar, barProto);
170engine->globalObject().setProperty("Bar", barCtor);
171//! [10]
172
173
174//! [11]
175static QScriptValue getSetFoo(QScriptContext *context, QScriptEngine *engine)
176{
177 QScriptValue callee = context->callee();
178 if (context->argumentCount() == 1) // writing?
179 callee.setProperty("value", context->argument(0));
180 return callee.property("value");
181}
182
183....
184
185QScriptValue object = engine.newObject();
186object.setProperty("foo", engine.newFunction(getSetFoo),
187 QScriptValue::PropertyGetter | QScriptValue::PropertySetter);
188//! [11]
189
190
191//! [12]
192QScriptValue object = engine.newObject();
193object.setProperty("foo", engine.newFunction(getFoo), QScriptValue::PropertyGetter);
194object.setProperty("foo", engine.newFunction(setFoo), QScriptValue::PropertySetter);
195//! [12]
196
197
198//! [13]
199Q_SCRIPT_DECLARE_QMETAOBJECT(QLineEdit, QWidget*)
200
201...
202
203QScriptValue lineEditClass = engine.scriptValueFromQMetaObject<QLineEdit>();
204engine.globalObject().setProperty("QLineEdit", lineEditClass);
205//! [13]
206
207
208//! [14]
209if (hello && world)
210 print("hello world");
211//! [14]
212
213
214//! [15]
215if (hello &&
216//! [15]
217
218
219//! [16]
2200 = 0
221//! [16]
222
223
224//! [17]
225./test.js
226//! [17]
227
228
229//! [18]
230foo["bar"]
231//! [18]
232
233
234//! [19]
235QScriptEngine engine;
236QScriptContext *context = engine.pushContext();
237context->activationObject().setProperty("myArg", 123);
238engine.evaluate("var tmp = myArg + 42");
239...
240engine.popContext();
241//! [19]
242
243
244//! [20]
245struct MyStruct {
246 int x;
247 int y;
248};
249//! [20]
250
251
252//! [21]
253Q_DECLARE_METATYPE(MyStruct)
254//! [21]
255
256
257//! [22]
258QScriptValue toScriptValue(QScriptEngine *engine, const MyStruct &s)
259{
260 QScriptValue obj = engine->newObject();
261 obj.setProperty("x", s.x);
262 obj.setProperty("y", s.y);
263 return obj;
264}
265
266void fromScriptValue(const QScriptValue &obj, MyStruct &s)
267{
268 s.x = obj.property("x").toInt32();
269 s.y = obj.property("y").toInt32();
270}
271//! [22]
272
273
274//! [23]
275qScriptRegisterMetaType(engine, toScriptValue, fromScriptValue);
276//! [23]
277
278
279//! [24]
280MyStruct s = qscriptvalue_cast<MyStruct>(context->argument(0));
281...
282MyStruct s2;
283s2.x = s.x + 10;
284s2.y = s.y + 20;
285QScriptValue v = engine->toScriptValue(s2);
286//! [24]
287
288
289//! [25]
290QScriptValue createMyStruct(QScriptContext *, QScriptEngine *engine)
291{
292 MyStruct s;
293 s.x = 123;
294 s.y = 456;
295 return engine->toScriptValue(s);
296}
297...
298QScriptValue ctor = engine.newFunction(createMyStruct);
299engine.globalObject().setProperty("MyStruct", ctor);
300//! [25]
301
302
303//! [26]
304Q_DECLARE_METATYPE(QVector<int>)
305
306...
307
308qScriptRegisterSequenceMetaType<QVector<int> >(engine);
309...
310QVector<int> v = qscriptvalue_cast<QVector<int> >(engine->evaluate("[5, 1, 3, 2]"));
311qSort(v.begin(), v.end());
312QScriptValue a = engine->toScriptValue(v);
313qDebug() << a.toString(); // outputs "[1, 2, 3, 5]"
314//! [26]
315
316//! [27]
317QScriptValue mySpecialQObjectConstructor(QScriptContext *context,
318 QScriptEngine *engine)
319{
320 QObject *parent = context->argument(0).toQObject();
321 QObject *object = new QObject(parent);
322 return engine->newQObject(object, QScriptEngine::ScriptOwnership);
323}
324
325...
326
327QScriptValue ctor = engine.newFunction(mySpecialQObjectConstructor);
328QScriptValue metaObject = engine.newQMetaObject(&QObject::staticMetaObject, ctor);
329engine.globalObject().setProperty("QObject", metaObject);
330
331QScriptValue result = engine.evaluate("new QObject()");
332//! [27]
Note: See TracBrowser for help on using the repository browser.