doc/src/snippets/code/src_script_qscriptengine.cpp
branchRCL_3
changeset 7 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 7:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 //! [0]
       
    43 QScriptEngine myEngine;
       
    44 QScriptValue three = myEngine.evaluate("1 + 2");
       
    45 //! [0]
       
    46 
       
    47 
       
    48 //! [1]
       
    49 QScriptValue fun = myEngine.evaluate("(function(a, b) { return a + b; })");
       
    50 QScriptValueList args;
       
    51 args << 1 << 2;
       
    52 QScriptValue threeAgain = fun.call(QScriptValue(), args);
       
    53 //! [1]
       
    54 
       
    55 
       
    56 //! [2]
       
    57 QString fileName = "helloworld.qs";
       
    58 QFile scriptFile(fileName);
       
    59 if (!scriptFile.open(QIODevice::ReadOnly))
       
    60     // handle error
       
    61 QTextStream stream(&scriptFile);
       
    62 QString contents = stream.readAll();
       
    63 scriptFile.close();
       
    64 myEngine.evaluate(contents, fileName);
       
    65 //! [2]
       
    66 
       
    67 
       
    68 //! [3]
       
    69 myEngine.globalObject().setProperty("myNumber", 123);
       
    70 ...
       
    71 QScriptValue myNumberPlusOne = myEngine.evaluate("myNumber + 1");
       
    72 //! [3]
       
    73 
       
    74 
       
    75 //! [4]
       
    76 QScriptValue result = myEngine.evaluate(...);
       
    77 if (myEngine.hasUncaughtException()) {
       
    78     int line = myEngine.uncaughtExceptionLineNumber();
       
    79     qDebug() << "uncaught exception at line" << line << ":" << result.toString();
       
    80 }
       
    81 //! [4]
       
    82 
       
    83 
       
    84 //! [5]
       
    85 QPushButton button;
       
    86 QScriptValue scriptButton = myEngine.newQObject(&button);
       
    87 myEngine.globalObject().setProperty("button", scriptButton);
       
    88 
       
    89 myEngine.evaluate("button.checkable = true");
       
    90 
       
    91 qDebug() << scriptButton.property("checkable").toBoolean();
       
    92 scriptButton.property("show").call(); // call the show() slot
       
    93 //! [5]
       
    94 
       
    95 
       
    96 //! [6]
       
    97 QScriptValue 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]
       
   107 QScriptValue fun = myEngine.newFunction(myAdd);
       
   108 myEngine.globalObject().setProperty("myAdd", fun);
       
   109 //! [7]
       
   110 
       
   111 
       
   112 //! [8]
       
   113 QScriptValue result = myEngine.evaluate("myAdd(myNumber, 1)");
       
   114 //! [8]
       
   115 
       
   116 
       
   117 //! [9]
       
   118 QScriptValue 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 
       
   139 QScriptValue fooProto = engine->newObject();
       
   140 fooProto.setProperty("whatever", ...);
       
   141 engine->globalObject().setProperty("Foo", engine->newFunction(Foo, fooProto));
       
   142 //! [9]
       
   143 
       
   144 
       
   145 //! [10]
       
   146 class Bar { ... };
       
   147 
       
   148 Q_DECLARE_METATYPE(Bar)
       
   149 
       
   150 QScriptValue 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 
       
   158 class 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
       
   167 BarPrototype *barPrototypeObject = new BarPrototype(...);
       
   168 QScriptValue barProto = engine->newQObject(barPrototypeObject);
       
   169 engine->setDefaultPrototype(qMetaTypeId<Bar>, barProto);
       
   170 QScriptValue barCtor = engine->newFunction(constructBar, barProto);
       
   171 engine->globalObject().setProperty("Bar", barCtor);
       
   172 //! [10]
       
   173 
       
   174 
       
   175 //! [11]
       
   176 static 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 
       
   186 QScriptValue object = engine.newObject();
       
   187 object.setProperty("foo", engine.newFunction(getSetFoo),
       
   188     QScriptValue::PropertyGetter | QScriptValue::PropertySetter);
       
   189 //! [11]
       
   190 
       
   191 
       
   192 //! [12]
       
   193 QScriptValue object = engine.newObject();
       
   194 object.setProperty("foo", engine.newFunction(getFoo), QScriptValue::PropertyGetter);
       
   195 object.setProperty("foo", engine.newFunction(setFoo), QScriptValue::PropertySetter);
       
   196 //! [12]
       
   197 
       
   198 
       
   199 //! [13]
       
   200 Q_SCRIPT_DECLARE_QMETAOBJECT(QLineEdit, QWidget*)
       
   201 
       
   202 ...
       
   203 
       
   204 QScriptValue lineEditClass = engine.scriptValueFromQMetaObject<QLineEdit>();
       
   205 engine.globalObject().setProperty("QLineEdit", lineEditClass);
       
   206 //! [13]
       
   207 
       
   208 
       
   209 //! [14]
       
   210 if (hello && world)
       
   211     print("hello world");
       
   212 //! [14]
       
   213 
       
   214 
       
   215 //! [15]
       
   216 if (hello &&
       
   217 //! [15]
       
   218 
       
   219 
       
   220 //! [16]
       
   221 0 = 0
       
   222 //! [16]
       
   223 
       
   224 
       
   225 //! [17]
       
   226 ./test.js
       
   227 //! [17]
       
   228 
       
   229 
       
   230 //! [18]
       
   231 foo["bar"]
       
   232 //! [18]
       
   233 
       
   234 
       
   235 //! [19]
       
   236 QScriptEngine engine;
       
   237 QScriptContext *context = engine.pushContext();
       
   238 context->activationObject().setProperty("myArg", 123);
       
   239 engine.evaluate("var tmp = myArg + 42");
       
   240 ...
       
   241 engine.popContext();
       
   242 //! [19]
       
   243 
       
   244 
       
   245 //! [20]
       
   246 struct MyStruct {
       
   247     int x;
       
   248     int y;
       
   249 };
       
   250 //! [20]
       
   251 
       
   252 
       
   253 //! [21]
       
   254 Q_DECLARE_METATYPE(MyStruct)
       
   255 //! [21]
       
   256 
       
   257 
       
   258 //! [22]
       
   259 QScriptValue 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 
       
   267 void 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]
       
   276 qScriptRegisterMetaType(engine, toScriptValue, fromScriptValue);
       
   277 //! [23]
       
   278 
       
   279 
       
   280 //! [24]
       
   281 MyStruct s = qscriptvalue_cast<MyStruct>(context->argument(0));
       
   282 ...
       
   283 MyStruct s2;
       
   284 s2.x = s.x + 10;
       
   285 s2.y = s.y + 20;
       
   286 QScriptValue v = engine->toScriptValue(s2);
       
   287 //! [24]
       
   288 
       
   289 
       
   290 //! [25]
       
   291 QScriptValue createMyStruct(QScriptContext *, QScriptEngine *engine)
       
   292 {
       
   293     MyStruct s;
       
   294     s.x = 123;
       
   295     s.y = 456;
       
   296     return engine->toScriptValue(s);
       
   297 }
       
   298 ...
       
   299 QScriptValue ctor = engine.newFunction(createMyStruct);
       
   300 engine.globalObject().setProperty("MyStruct", ctor);
       
   301 //! [25]
       
   302 
       
   303 
       
   304 //! [26]
       
   305 Q_DECLARE_METATYPE(QVector<int>)
       
   306 
       
   307 ...
       
   308 
       
   309 qScriptRegisterSequenceMetaType<QVector<int> >(engine);
       
   310 ...
       
   311 QVector<int> v = qscriptvalue_cast<QVector<int> >(engine->evaluate("[5, 1, 3, 2]"));
       
   312 qSort(v.begin(), v.end());
       
   313 QScriptValue a = engine->toScriptValue(v);
       
   314 qDebug() << a.toString(); // outputs "[1, 2, 3, 5]"
       
   315 //! [26]
       
   316 
       
   317 //! [27]
       
   318 QScriptValue 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 
       
   328 QScriptValue ctor = engine.newFunction(mySpecialQObjectConstructor);
       
   329 QScriptValue metaObject = engine.newQMetaObject(&QObject::staticMetaObject, ctor);
       
   330 engine.globalObject().setProperty("QObject", metaObject);
       
   331 
       
   332 QScriptValue result = engine.evaluate("new QObject()");
       
   333 //! [27]