| 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 | /*!
|
|---|
| 43 | \group script
|
|---|
| 44 | \title Scripting Classes and Overviews
|
|---|
| 45 |
|
|---|
| 46 | \brief Classes that add scripting capabilities to Qt applications.
|
|---|
| 47 | */
|
|---|
| 48 |
|
|---|
| 49 | /*!
|
|---|
| 50 | \page scripting.html
|
|---|
| 51 | \title Making Applications Scriptable
|
|---|
| 52 | \ingroup frameworks-technologies
|
|---|
| 53 |
|
|---|
| 54 | Qt 4.3 and later provides support for application scripting with ECMAScript.
|
|---|
| 55 | The following guides and references cover aspects of programming with
|
|---|
| 56 | ECMAScript and Qt.
|
|---|
| 57 |
|
|---|
| 58 | \tableofcontents
|
|---|
| 59 |
|
|---|
| 60 | \section1 Scripting Classes
|
|---|
| 61 |
|
|---|
| 62 | The following classes add scripting capabilities to Qt applications.
|
|---|
| 63 |
|
|---|
| 64 | \annotatedlist script
|
|---|
| 65 |
|
|---|
| 66 | \section1 Language Overview
|
|---|
| 67 |
|
|---|
| 68 | Qt Script is based on the ECMAScript scripting language, as defined
|
|---|
| 69 | in standard \l{ECMA-262}. Microsoft's JScript, and Netscape's
|
|---|
| 70 | JavaScript are also based on the ECMAScript standard. For an
|
|---|
| 71 | overview of ECMAScript, see the
|
|---|
| 72 | \l{ECMAScript Reference}{ECMAScript reference}.
|
|---|
| 73 | If you are not familiar with the ECMAScript language, there are
|
|---|
| 74 | several existing tutorials and books that cover this subject, such
|
|---|
| 75 | as \l{JavaScript: The Definitive Guide}.
|
|---|
| 76 |
|
|---|
| 77 | Existing users of \l{Qt Script for Applications (QSA)} may find the
|
|---|
| 78 | \l{Moving from QSA to Qt Script} document useful when porting
|
|---|
| 79 | QSA scripts to Qt Script.
|
|---|
| 80 |
|
|---|
| 81 | \section1 Basic Usage
|
|---|
| 82 |
|
|---|
| 83 | To evaluate script code, you create a QScriptEngine and call its
|
|---|
| 84 | evaluate() function, passing the script code (text) to evaluate
|
|---|
| 85 | as argument.
|
|---|
| 86 |
|
|---|
| 87 | \snippet doc/src/snippets/qtscript/evaluation/main.cpp 0
|
|---|
| 88 |
|
|---|
| 89 | The return value will be the result of the evaluation (represented
|
|---|
| 90 | as a QScriptValue object); this can be converted to standard C++
|
|---|
| 91 | and Qt types.
|
|---|
| 92 |
|
|---|
| 93 | Custom properties can be made available to scripts by registering
|
|---|
| 94 | them with the script engine. This is most easily done by setting
|
|---|
| 95 | properties of the script engine's \e{Global Object}:
|
|---|
| 96 |
|
|---|
| 97 | \snippet doc/src/snippets/qtscript/registeringvalues/main.cpp 0
|
|---|
| 98 |
|
|---|
| 99 | This places the properties in the script environment, thus making them
|
|---|
| 100 | available to script code.
|
|---|
| 101 |
|
|---|
| 102 | \section1 Making a QObject Available to the Script Engine
|
|---|
| 103 |
|
|---|
| 104 | Any QObject-based instance can be made available for use with scripts.
|
|---|
| 105 |
|
|---|
| 106 | When a QObject is passed to the QScriptEngine::newQObject() function,
|
|---|
| 107 | a Qt Script wrapper object is created that can be used to make the
|
|---|
| 108 | QObject's signals, slots, properties, and child objects available
|
|---|
| 109 | to scripts.
|
|---|
| 110 |
|
|---|
| 111 | Here's an example of making an instance of a QObject subclass
|
|---|
| 112 | available to script code under the name \c{"myObject"}:
|
|---|
| 113 |
|
|---|
| 114 | \snippet doc/src/snippets/qtscript/registeringobjects/main.cpp 0
|
|---|
| 115 |
|
|---|
| 116 | This will create a global variable called \c{myObject} in the
|
|---|
| 117 | script environment. The variable serves as a proxy to the
|
|---|
| 118 | underlying C++ object. Note that the name of the script variable
|
|---|
| 119 | can be anything; i.e., it is not dependent upon QObject::objectName().
|
|---|
| 120 |
|
|---|
| 121 | The \l{QScriptEngine::}{newQObject()} function accepts two additional
|
|---|
| 122 | optional arguments: one is the ownership mode, and the other is a
|
|---|
| 123 | collection of options that allow you to control certain aspects of how
|
|---|
| 124 | the QScriptValue that wraps the QObject should behave. We will come
|
|---|
| 125 | back to the usage of these arguments later.
|
|---|
| 126 |
|
|---|
| 127 | \section2 Using Signals and Slots
|
|---|
| 128 |
|
|---|
| 129 | Qt Script adapts Qt's central \l{Signals and Slots} feature for
|
|---|
| 130 | scripting. There are three principal ways to use signals and slots
|
|---|
| 131 | with Qt Script:
|
|---|
| 132 |
|
|---|
| 133 | \list
|
|---|
| 134 | \i \bold{Hybrid C++/script}: C++ application code connects a
|
|---|
| 135 | signal to a script function. The script function can, for example, be
|
|---|
| 136 | a function that the user has typed in, or one that you have read from a
|
|---|
| 137 | file. This approach is useful if you have a QObject but don't want
|
|---|
| 138 | to expose the object itself to the scripting environment; you just
|
|---|
| 139 | want a script to be able to define how a signal should be reacted
|
|---|
| 140 | to, and leave it up to the C++ side of your application to establish
|
|---|
| 141 | the connection.
|
|---|
| 142 |
|
|---|
| 143 | \i \bold{Hybrid script/C++}: A script can connect signals and slots
|
|---|
| 144 | to establish connections between pre-defined objects that the
|
|---|
| 145 | application exposes to the scripting environment. In this scenario,
|
|---|
| 146 | the slots themselves are still written in C++, but the definition of
|
|---|
| 147 | the connections is fully dynamic (script-defined).
|
|---|
| 148 |
|
|---|
| 149 | \i \bold{Purely script-defined}: A script can both define signal
|
|---|
| 150 | handler functions (effectively "slots written in Qt Script"),
|
|---|
| 151 | \e{and} set up the connections that utilize those handlers. For
|
|---|
| 152 | example, a script can define a function that will handle the
|
|---|
| 153 | QLineEdit::returnPressed() signal, and then connect that signal to the
|
|---|
| 154 | script function.
|
|---|
| 155 | \endlist
|
|---|
| 156 |
|
|---|
| 157 | Use the qScriptConnect() function to connect a C++ signal to a
|
|---|
| 158 | script function. In the following example a script signal handler is
|
|---|
| 159 | defined that will handle the QLineEdit::textChanged() signal:
|
|---|
| 160 |
|
|---|
| 161 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 47
|
|---|
| 162 |
|
|---|
| 163 | The first two arguments to qScriptConnect() are the same
|
|---|
| 164 | as you would pass to QObject::connect() to establish a normal C++
|
|---|
| 165 | connection. The third argument is the script object that will act
|
|---|
| 166 | as the \c this object when the signal handler is invoked; in the above
|
|---|
| 167 | example we pass an invalid script value, so the \c this object will
|
|---|
| 168 | be the Global Object. The fourth argument is the script function
|
|---|
| 169 | ("slot") itself. The following example shows how the \c this argument
|
|---|
| 170 | can be put to use:
|
|---|
| 171 |
|
|---|
| 172 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 48
|
|---|
| 173 |
|
|---|
| 174 | We create two QLineEdit objects and define a single signal handler
|
|---|
| 175 | function. The connections use the same handler function, but the
|
|---|
| 176 | function will be invoked with a different \c this object depending on
|
|---|
| 177 | which object's signal was triggered, so the output of the print()
|
|---|
| 178 | statement will be different for each.
|
|---|
| 179 |
|
|---|
| 180 | In script code, Qt Script uses a different syntax for connecting to
|
|---|
| 181 | and disconnecting from signals than the familiar C++ syntax; i.e.,
|
|---|
| 182 | QObject::connect().
|
|---|
| 183 | To connect to a signal, you reference the relevant signal as a property
|
|---|
| 184 | of the sender object, and invoke its \c{connect()} function. There
|
|---|
| 185 | are three overloads of \c{connect()}, each with a corresponding
|
|---|
| 186 | \c{disconnect()} overload. The following subsections describe these
|
|---|
| 187 | three forms.
|
|---|
| 188 |
|
|---|
| 189 | \section3 Signal to Function Connections
|
|---|
| 190 |
|
|---|
| 191 | \c{connect(function)}
|
|---|
| 192 |
|
|---|
| 193 | In this form of connection, the argument to \c{connect()} is the
|
|---|
| 194 | function to connect to the signal.
|
|---|
| 195 |
|
|---|
| 196 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 2
|
|---|
| 197 |
|
|---|
| 198 | The argument can be a Qt Script function, as in the above
|
|---|
| 199 | example, or it can be a QObject slot, as in
|
|---|
| 200 | the following example:
|
|---|
| 201 |
|
|---|
| 202 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 3
|
|---|
| 203 |
|
|---|
| 204 | When the argument is a QObject slot, the argument types of the
|
|---|
| 205 | signal and slot do not necessarily have to be compatible;
|
|---|
| 206 | QtScript will, if necessary, perform conversion of the signal
|
|---|
| 207 | arguments to match the argument types of the slot.
|
|---|
| 208 |
|
|---|
| 209 | To disconnect from a signal, you invoke the signal's
|
|---|
| 210 | \c{disconnect()} function, passing the function to disconnect
|
|---|
| 211 | as argument:
|
|---|
| 212 |
|
|---|
| 213 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 4
|
|---|
| 214 |
|
|---|
| 215 | When a script function is invoked in response to a signal, the
|
|---|
| 216 | \c this object will be the Global Object.
|
|---|
| 217 |
|
|---|
| 218 | \section3 Signal to Member Function Connections
|
|---|
| 219 |
|
|---|
| 220 | \c{connect(thisObject, function)}
|
|---|
| 221 |
|
|---|
| 222 | In this form of the \c{connect()} function, the first argument
|
|---|
| 223 | is the object that will be bound to the variable, \c this, when
|
|---|
| 224 | the function specified using the second argument is invoked.
|
|---|
| 225 |
|
|---|
| 226 | If you have a push button in a form, you typically want to do
|
|---|
| 227 | something involving the form in response to the button's
|
|---|
| 228 | \c{clicked} signal; passing the form as the \c this object
|
|---|
| 229 | makes sense in such a case.
|
|---|
| 230 |
|
|---|
| 231 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 5
|
|---|
| 232 |
|
|---|
| 233 | To disconnect from the signal, pass the same arguments to \c{disconnect()}:
|
|---|
| 234 |
|
|---|
| 235 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 6
|
|---|
| 236 |
|
|---|
| 237 | \section3 Signal to Named Member Function Connections
|
|---|
| 238 |
|
|---|
| 239 | \c{connect(thisObject, functionName)}
|
|---|
| 240 |
|
|---|
| 241 | In this form of the \c{connect()} function, the first argument is
|
|---|
| 242 | the object that will be bound to the variable, \c this, when
|
|---|
| 243 | a function is invoked in response to the signal. The second argument
|
|---|
| 244 | specifies the name of a function that is connected to the signal,
|
|---|
| 245 | and this refers to a member function of the object passed as the
|
|---|
| 246 | first argument (\c thisObject in the above scheme).
|
|---|
| 247 |
|
|---|
| 248 | Note that the function is resolved when the connection is made, not
|
|---|
| 249 | when the signal is emitted.
|
|---|
| 250 |
|
|---|
| 251 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 7
|
|---|
| 252 |
|
|---|
| 253 | To disconnect from the signal, pass the same arguments to \c{disconnect()}:
|
|---|
| 254 |
|
|---|
| 255 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 8
|
|---|
| 256 |
|
|---|
| 257 | \section3 Error Handling
|
|---|
| 258 |
|
|---|
| 259 | When \c{connect()} or \c{disconnect()} succeeds, the function will
|
|---|
| 260 | return \c{undefined}; otherwise, it will throw a script exception.
|
|---|
| 261 | You can obtain an error message from the resulting \c{Error} object.
|
|---|
| 262 | Example:
|
|---|
| 263 |
|
|---|
| 264 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 9
|
|---|
| 265 |
|
|---|
| 266 | \section3 Emitting Signals from Scripts
|
|---|
| 267 |
|
|---|
| 268 | To emit a signal from script code, you simply invoke the signal
|
|---|
| 269 | function, passing the relevant arguments:
|
|---|
| 270 |
|
|---|
| 271 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 10
|
|---|
| 272 |
|
|---|
| 273 | It is currently not possible to define a new signal in a script;
|
|---|
| 274 | i.e., all signals must be defined by C++ classes.
|
|---|
| 275 |
|
|---|
| 276 | \section3 Overloaded Signals and Slots
|
|---|
| 277 |
|
|---|
| 278 | When a signal or slot is overloaded, QtScript will attempt to
|
|---|
| 279 | pick the right overload based on the actual types of the QScriptValue arguments
|
|---|
| 280 | involved in the function invocation. For example, if your class has slots
|
|---|
| 281 | \c{myOverloadedSlot(int)} and \c{myOverloadedSlot(QString)}, the following
|
|---|
| 282 | script code will behave reasonably:
|
|---|
| 283 |
|
|---|
| 284 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 11
|
|---|
| 285 |
|
|---|
| 286 | You can specify a particular overload by using array-style property access
|
|---|
| 287 | with the \l{QMetaObject::normalizedSignature()}{normalized signature} of
|
|---|
| 288 | the C++ function as the property name:
|
|---|
| 289 |
|
|---|
| 290 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 12
|
|---|
| 291 |
|
|---|
| 292 | If the overloads have different number of arguments, QtScript will
|
|---|
| 293 | pick the overload with the argument count that best matches the
|
|---|
| 294 | actual number of arguments passed to the slot.
|
|---|
| 295 |
|
|---|
| 296 | For overloaded signals, Qt Script will throw an error if you try to connect
|
|---|
| 297 | to the signal by name; you have to refer to the signal with the full
|
|---|
| 298 | normalized signature of the particular overload you want to connect to.
|
|---|
| 299 |
|
|---|
| 300 | \section2 Accessing Properties
|
|---|
| 301 |
|
|---|
| 302 | The properties of the QObject are available as properties
|
|---|
| 303 | of the corresponding QtScript object. When you manipulate
|
|---|
| 304 | a property in script code, the C++ get/set method for that
|
|---|
| 305 | property will automatically be invoked. For example, if your
|
|---|
| 306 | C++ class has a property declared as follows:
|
|---|
| 307 |
|
|---|
| 308 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 13
|
|---|
| 309 |
|
|---|
| 310 | then script code can do things like the following:
|
|---|
| 311 |
|
|---|
| 312 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 14
|
|---|
| 313 |
|
|---|
| 314 | \section2 Accessing Child QObjects
|
|---|
| 315 |
|
|---|
| 316 | Every named child of the QObject (that is, for which
|
|---|
| 317 | QObject::objectName() is not an empty string) is by default available as
|
|---|
| 318 | a property of the QtScript wrapper object. For example,
|
|---|
| 319 | if you have a QDialog with a child widget whose \c{objectName} property is
|
|---|
| 320 | \c{"okButton"}, you can access this object in script code through
|
|---|
| 321 | the expression
|
|---|
| 322 |
|
|---|
| 323 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 15
|
|---|
| 324 |
|
|---|
| 325 | Since \c{objectName} is itself a Q_PROPERTY, you can manipulate
|
|---|
| 326 | the name in script code to, for example, rename an object:
|
|---|
| 327 |
|
|---|
| 328 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 16
|
|---|
| 329 |
|
|---|
| 330 | You can also use the functions \c{findChild()} and \c{findChildren()}
|
|---|
| 331 | to find children. These two functions behave identically to
|
|---|
| 332 | QObject::findChild() and QObject::findChildren(), respectively.
|
|---|
| 333 |
|
|---|
| 334 | For example, we can use these functions to find objects using strings
|
|---|
| 335 | and regular expressions:
|
|---|
| 336 |
|
|---|
| 337 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 17
|
|---|
| 338 |
|
|---|
| 339 | You typically want to use \c{findChild()} when manipulating a form
|
|---|
| 340 | that uses nested layouts; that way the script is isolated from the
|
|---|
| 341 | details about which particular layout a widget is located in.
|
|---|
| 342 |
|
|---|
| 343 | \section2 Controlling QObject Ownership
|
|---|
| 344 |
|
|---|
| 345 | Qt Script uses garbage collection to reclaim memory used by script
|
|---|
| 346 | objects when they are no longer needed; an object's memory can be
|
|---|
| 347 | automatically reclaimed when it is no longer referenced anywhere in
|
|---|
| 348 | the scripting environment. Qt Script lets you control what happens
|
|---|
| 349 | to the underlying C++ QObject when the wrapper object is reclaimed
|
|---|
| 350 | (i.e., whether the QObject is deleted or not); you do this when you
|
|---|
| 351 | create an object by passing an ownership mode as the second argument
|
|---|
| 352 | to QScriptEngine::newQObject().
|
|---|
| 353 |
|
|---|
| 354 | Knowing how Qt Script deals with ownership is important, since it can
|
|---|
| 355 | help you avoid situations where a C++ object isn't deleted when it
|
|---|
| 356 | should be (causing memory leaks), or where a C++ object \e{is}
|
|---|
| 357 | deleted when it shouldn't be (typically causing a crash if C++ code
|
|---|
| 358 | later tries to access that object).
|
|---|
| 359 |
|
|---|
| 360 | \section3 Qt Ownership
|
|---|
| 361 |
|
|---|
| 362 | By default, the script engine does not take ownership of the
|
|---|
| 363 | QObject that is passed to QScriptEngine::newQObject(); the object
|
|---|
| 364 | is managed according to Qt's object ownership (see
|
|---|
| 365 | \l{Object Trees and Object Ownership}). This mode is appropriate
|
|---|
| 366 | when, for example, you are wrapping C++ objects that are part of
|
|---|
| 367 | your application's core; that is, they should persist regardless of
|
|---|
| 368 | what happens in the scripting environment. Another way of stating
|
|---|
| 369 | this is that the C++ objects should outlive the script engine.
|
|---|
| 370 |
|
|---|
| 371 | \section3 Script Ownership
|
|---|
| 372 |
|
|---|
| 373 | Specifying QScriptEngine::ScriptOwnership as the ownership mode
|
|---|
| 374 | will cause the script engine to take full ownership of the QObject
|
|---|
| 375 | and delete it when it determines that it is safe to do so
|
|---|
| 376 | (i.e., when there are no more references to it in script code).
|
|---|
| 377 | This ownership mode is appropriate if the QObject does not have a
|
|---|
| 378 | parent object, and/or the QObject is created in the context of the
|
|---|
| 379 | script engine and is not intended to outlive the script engine.
|
|---|
| 380 |
|
|---|
| 381 | For example, a constructor function that constructs QObjects
|
|---|
| 382 | only to be used in the script environment is a good candidate:
|
|---|
| 383 |
|
|---|
| 384 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 18
|
|---|
| 385 |
|
|---|
| 386 | \section3 Auto-Ownership
|
|---|
| 387 |
|
|---|
| 388 | With QScriptEngine::AutoOwnership the ownership is based on whether
|
|---|
| 389 | the QObject has a parent or not.
|
|---|
| 390 | If the QtScript garbage collector finds that the QObject is no
|
|---|
| 391 | longer referenced within the script environment, the QObject will
|
|---|
| 392 | be deleted \e{only} if it does not have a parent.
|
|---|
| 393 |
|
|---|
| 394 | \section3 What Happens When Someone Else Deletes the QObject?
|
|---|
| 395 |
|
|---|
| 396 | It is possible that a wrapped QObject is deleted outside of
|
|---|
| 397 | Qt Script's control; i.e., without regard to the ownership mode
|
|---|
| 398 | specified. In this case, the wrapper object will still
|
|---|
| 399 | be an object (unlike the C++ pointer it wraps, the script object
|
|---|
| 400 | won't become null). Any attempt to access properties of the script
|
|---|
| 401 | object will, however, result in a script exception being thrown.
|
|---|
| 402 |
|
|---|
| 403 | Note that QScriptValue::isQObject() will still return true for a
|
|---|
| 404 | deleted QObject, since it tests the type of the script object, not
|
|---|
| 405 | whether the internal pointer is non-null. In other words, if
|
|---|
| 406 | QScriptValue::isQObject() returns true but QScriptValue::toQObject()
|
|---|
| 407 | returns a null pointer, this indicates that the QObject has been
|
|---|
| 408 | deleted outside of Qt Script (perhaps accidentally).
|
|---|
| 409 |
|
|---|
| 410 | \section2 Customizing Access to the QObject
|
|---|
| 411 |
|
|---|
| 412 | QScriptEngine::newQObject() can take a third argument which allows
|
|---|
| 413 | you to control various aspects of the access to the QObject through
|
|---|
| 414 | the QtScript wrapper object it returns.
|
|---|
| 415 |
|
|---|
| 416 | QScriptEngine::ExcludeChildObjects specifies that child objects of
|
|---|
| 417 | the QObject should not appear as properties of the wrapper object.
|
|---|
| 418 |
|
|---|
| 419 | QScriptEngine::ExcludeSuperClassProperties and
|
|---|
| 420 | QScriptEngine::ExcludeSuperClassMethods can be used to avoid
|
|---|
| 421 | exposing members that are inherited from the QObject's superclass.
|
|---|
| 422 | This is useful for defining a "pure" interface where inherited members
|
|---|
| 423 | don't make sense from a scripting perspective; e.g., you don't want
|
|---|
| 424 | script authors to be able to change the \c{objectName} property of
|
|---|
| 425 | the object or invoke the \c{deleteLater()} slot.
|
|---|
| 426 |
|
|---|
| 427 | QScriptEngine::AutoCreateDynamicProperties specifies that properties
|
|---|
| 428 | that don't already exist in the QObject should be created as dynamic
|
|---|
| 429 | properties of the QObject, rather than as properties of the QtScript
|
|---|
| 430 | wrapper object. If you want new properties to truly become persistent
|
|---|
| 431 | properties of the QObject, rather than properties that are destroyed
|
|---|
| 432 | along with the wrapper object (and that aren't shared if the QObject
|
|---|
| 433 | is wrapped multiple times with QScriptEngine::newQObject()), you
|
|---|
| 434 | should use this option.
|
|---|
| 435 |
|
|---|
| 436 | QScriptEngine::SkipMethodsInEnumeration specifies that signals and
|
|---|
| 437 | slots should be skipped when enumerating the properties of the QObject
|
|---|
| 438 | wrapper in a for-in script statement. This is useful when defining
|
|---|
| 439 | prototype objects, since by convention function properties of
|
|---|
| 440 | prototypes should not be enumerable.
|
|---|
| 441 |
|
|---|
| 442 | \section2 Making a QObject-based Class New-able from a Script
|
|---|
| 443 |
|
|---|
| 444 | The QScriptEngine::newQObject() function is used to wrap an
|
|---|
| 445 | existing QObject instance, so that it can be made available to
|
|---|
| 446 | scripts. A different scenario is that you want scripts to be
|
|---|
| 447 | able to construct new objects, not just access existing ones.
|
|---|
| 448 |
|
|---|
| 449 | The Qt meta-type system currently does not provide dynamic
|
|---|
| 450 | binding of constructors for QObject-based classes. If you want to
|
|---|
| 451 | make such a class new-able from scripts, Qt Script can generate
|
|---|
| 452 | a reasonable script constructor for you; see
|
|---|
| 453 | QScriptEngine::scriptValueFromQMetaObject().
|
|---|
| 454 |
|
|---|
| 455 | You can also use QScriptEngine::newFunction() to wrap your own
|
|---|
| 456 | factory function, and add it to the script environment; see
|
|---|
| 457 | QScriptEngine::newQMetaObject() for an example.
|
|---|
| 458 |
|
|---|
| 459 | \section2 Enum Values
|
|---|
| 460 |
|
|---|
| 461 | Values for enums declared with Q_ENUMS are not available as
|
|---|
| 462 | properties of individual wrapper objects; rather, they are
|
|---|
| 463 | properties of the QMetaObject wrapper object that can be created
|
|---|
| 464 | with QScriptEngine::newQMetaObject().
|
|---|
| 465 |
|
|---|
| 466 | \section1 Conversion Between QtScript and C++ Types
|
|---|
| 467 |
|
|---|
| 468 | QtScript will perform type conversion when a value needs to be
|
|---|
| 469 | converted from the script side to the C++ side or vice versa; for
|
|---|
| 470 | instance, when a C++ signal triggers a script function, when
|
|---|
| 471 | you access a QObject property in script code, or when
|
|---|
| 472 | you call QScriptEngine::toScriptValue() or
|
|---|
| 473 | QScriptEngine::fromScriptValue() in C++. QtScript provides default
|
|---|
| 474 | conversion operations for many of the built-in Qt types. You can
|
|---|
| 475 | change the conversion operation for a type (including your custom
|
|---|
| 476 | C++ types) by registering your own conversion functions with
|
|---|
| 477 | qScriptRegisterMetaType().
|
|---|
| 478 |
|
|---|
| 479 | \section2 Default Conversion from Qt Script to C++
|
|---|
| 480 |
|
|---|
| 481 | The following table describes the default conversion from a
|
|---|
| 482 | QScriptValue to a C++ type.
|
|---|
| 483 |
|
|---|
| 484 | \table 80%
|
|---|
| 485 | \header \o C++ Type \o Default Conversion
|
|---|
| 486 | \row \o bool \o QScriptValue::toBool()
|
|---|
| 487 | \row \o int \o QScriptValue::toInt32()
|
|---|
| 488 | \row \o uint \o QScriptValue::toUInt32()
|
|---|
| 489 | \row \o float \o float(QScriptValue::toNumber())
|
|---|
| 490 | \row \o double \o QScriptValue::toNumber()
|
|---|
| 491 | \row \o short \o short(QScriptValue::toInt32())
|
|---|
| 492 | \row \o ushort \o QScriptValue::toUInt16()
|
|---|
| 493 | \row \o char \o char(QScriptValue::toInt32())
|
|---|
| 494 | \row \o uchar \o unsigned char(QScriptValue::toInt32())
|
|---|
| 495 | \row \o qlonglong \o qlonglong(QScriptValue::toInteger())
|
|---|
| 496 | \row \o qulonglong \o qulonglong(QScriptValue::toInteger())
|
|---|
| 497 | \row \o QString \o An empty string if the QScriptValue is null
|
|---|
| 498 | or undefined; QScriptValue::toString() otherwise.
|
|---|
| 499 | \row \o QDateTime \o QScriptValue::toDateTime()
|
|---|
| 500 | \row \o QDate \o QScriptValue::toDateTime().date()
|
|---|
| 501 | \row \o QRegExp \o QScriptValue::toRegExp()
|
|---|
| 502 | \row \o QObject* \o QScriptValue::toQObject()
|
|---|
| 503 | \row \o QWidget* \o QScriptValue::toQObject()
|
|---|
| 504 | \row \o QVariant \o QScriptValue::toVariant()
|
|---|
| 505 | \row \o QChar \o If the QScriptValue is a string, the result
|
|---|
| 506 | is the first character of the string, or a null QChar
|
|---|
| 507 | if the string is empty; otherwise, the result is a QChar
|
|---|
| 508 | constructed from the unicode obtained by converting the
|
|---|
| 509 | QScriptValue to a \c{ushort}.
|
|---|
| 510 | \row \o QStringList \o If the QScriptValue is an array, the
|
|---|
| 511 | result is a QStringList constructed from the result of
|
|---|
| 512 | QScriptValue::toString() for each array element; otherwise,
|
|---|
| 513 | the result is an empty QStringList.
|
|---|
| 514 | \row \o QVariantList \o If the QScriptValue is an array, the result
|
|---|
| 515 | is a QVariantList constructed from the result of
|
|---|
| 516 | QScriptValue::toVariant() for each array element; otherwise,
|
|---|
| 517 | the result is an empty QVariantList.
|
|---|
| 518 | \row \o QVariantMap \o If the QScriptValue is an object, the result
|
|---|
| 519 | is a QVariantMap with a (key, value) pair of the form
|
|---|
| 520 | (propertyName, propertyValue.toVariant()) for each property,
|
|---|
| 521 | using QScriptValueIterator to iterate over the object's
|
|---|
| 522 | properties.
|
|---|
| 523 | \row \o QObjectList \o If the QScriptValue is an array, the result
|
|---|
| 524 | is a QObjectList constructed from the result of
|
|---|
| 525 | QScriptValue::toQObject() for each array element; otherwise,
|
|---|
| 526 | the result is an empty QObjectList.
|
|---|
| 527 | \row \o QList<int> \o If the QScriptValue is an array, the result is
|
|---|
| 528 | a QList<int> constructed from the result of
|
|---|
| 529 | QScriptValue::toInt32() for each array element; otherwise,
|
|---|
| 530 | the result is an empty QList<int>.
|
|---|
| 531 | \endtable
|
|---|
| 532 |
|
|---|
| 533 | Additionally, QtScript will handle the following cases:
|
|---|
| 534 |
|
|---|
| 535 | \list
|
|---|
| 536 | \i If the QScriptValue is a QObject and the target type name ends with
|
|---|
| 537 | \c * (i.e., it is a pointer), the QObject pointer will be cast to the
|
|---|
| 538 | target type with qobject_cast().
|
|---|
| 539 | \i If the QScriptValue is a QVariant and the target type name ends with
|
|---|
| 540 | \c * (i.e., it is a pointer), and the \l{QVariant::userType()}{userType()}
|
|---|
| 541 | of the QVariant is the type that the target type points to, the result
|
|---|
| 542 | is a pointer to the QVariant's data.
|
|---|
| 543 | \i If the QScriptValue is a QVariant and it can be converted to the
|
|---|
| 544 | target type (according to QVariant::canConvert()), the QVariant will
|
|---|
| 545 | be cast to the target type with qvariant_cast().
|
|---|
| 546 | \endlist
|
|---|
| 547 |
|
|---|
| 548 | \section2 Default Conversion from C++ to Qt Script
|
|---|
| 549 |
|
|---|
| 550 | The following table describes the default behavior when a QScriptValue is
|
|---|
| 551 | constructed from a C++ type:
|
|---|
| 552 |
|
|---|
| 553 | \table 80%
|
|---|
| 554 | \header \o C++ Type \o Default Construction
|
|---|
| 555 | \row \o void \o QScriptEngine::undefinedValue()
|
|---|
| 556 | \row \o bool \o QScriptValue(engine, value)
|
|---|
| 557 | \row \o int \o QScriptValue(engine, value)
|
|---|
| 558 | \row \o uint \o QScriptValue(engine, value)
|
|---|
| 559 | \row \o float \o QScriptValue(engine, value)
|
|---|
| 560 | \row \o double \o QScriptValue(engine, value)
|
|---|
| 561 | \row \o short \o QScriptValue(engine, value)
|
|---|
| 562 | \row \o ushort \o QScriptValue(engine, value)
|
|---|
| 563 | \row \o char \o QScriptValue(engine, value)
|
|---|
| 564 | \row \o uchar \o QScriptValue(engine, value)
|
|---|
| 565 | \row \o QString \o QScriptValue(engine, value)
|
|---|
| 566 | \row \o qlonglong \o QScriptValue(engine, qsreal(value)). Note that
|
|---|
| 567 | the conversion may lead to loss of precision, since not all
|
|---|
| 568 | 64-bit integers can be represented using the qsreal type.
|
|---|
| 569 | \row \o qulonglong \o QScriptValue(engine, qsreal(value)). Note that
|
|---|
| 570 | the conversion may lead to loss of precision, since not all
|
|---|
| 571 | 64-bit unsigned integers can be represented using the qsreal
|
|---|
| 572 | type.
|
|---|
| 573 | \row \o QChar \o QScriptValue(this, value.unicode())
|
|---|
| 574 | \row \o QDateTime \o \l{QScriptEngine::newDate()}{QScriptEngine::newDate}(value)
|
|---|
| 575 | \row \o QDate \o \l{QScriptEngine::newDate()}{QScriptEngine::newDate}(value)
|
|---|
| 576 | \row \o QRegExp \o \l{QScriptEngine::newRegExp()}{QScriptEngine::newRegExp}(value)
|
|---|
| 577 | \row \o QObject* \o \l{QScriptEngine::newQObject()}{QScriptEngine::newQObject}(value)
|
|---|
| 578 | \row \o QWidget* \o \l{QScriptEngine::newQObject()}{QScriptEngine::newQObject}(value)
|
|---|
| 579 | \row \o QVariant \o \l{QScriptEngine::newVariant()}{QScriptEngine::newVariant}(value)
|
|---|
| 580 | \row \o QStringList \o A new script array (created with
|
|---|
| 581 | QScriptEngine::newArray()), whose elements are created using
|
|---|
| 582 | the QScriptValue(QScriptEngine *, QString) constructor for
|
|---|
| 583 | each element of the list.
|
|---|
| 584 | \row \o QVariantList \o A new script array (created with
|
|---|
| 585 | QScriptEngine::newArray()), whose elements are created using
|
|---|
| 586 | QScriptEngine::newVariant() for each element of the list.
|
|---|
| 587 | \row \o QVariantMap \o A new script object (created with
|
|---|
| 588 | QScriptEngine::newObject()), whose properties are initialized
|
|---|
| 589 | according to the (key, value) pairs of the map.
|
|---|
| 590 | \row \o QObjectList \o A new script array (created with
|
|---|
| 591 | QScriptEngine::newArray()), whose elements are created using
|
|---|
| 592 | QScriptEngine::newQObject() for each element of the list.
|
|---|
| 593 | \row \o QList<int> \o A new script array (created with
|
|---|
| 594 | QScriptEngine::newArray()), whose elements are created using
|
|---|
| 595 | the QScriptValue(QScriptEngine *, int) constructor for each
|
|---|
| 596 | element of the list.
|
|---|
| 597 | \endtable
|
|---|
| 598 |
|
|---|
| 599 | Other types (including custom types) will be wrapped using
|
|---|
| 600 | QScriptEngine::newVariant(). For null pointers of any type, the
|
|---|
| 601 | result is QScriptEngine::nullValue().
|
|---|
| 602 |
|
|---|
| 603 | \section1 How to Design and Implement Application Objects
|
|---|
| 604 |
|
|---|
| 605 | This section explains how to implement application objects and
|
|---|
| 606 | provides the necessary technical background material.
|
|---|
| 607 |
|
|---|
| 608 | \section2 Making a C++ object available to Scripts Written in QtScript
|
|---|
| 609 |
|
|---|
| 610 | Making C++ classes and objects available to a scripting language is
|
|---|
| 611 | not trivial because scripting languages tend to be more dynamic than
|
|---|
| 612 | C++, and it must be possible to introspect objects (query information
|
|---|
| 613 | such as function names, function signatures, properties, etc., at
|
|---|
| 614 | run-time). Standard C++ does not provide features for this.
|
|---|
| 615 |
|
|---|
| 616 | We can achieve the functionality we want by extending C++, using
|
|---|
| 617 | C++'s own facilities so our code is still standard C++. The Qt
|
|---|
| 618 | meta-object system provides the necessary additional functionality.
|
|---|
| 619 | It allows us to write using an extended C++ syntax, but converts this
|
|---|
| 620 | into standard C++ using a small utility program called \l{moc}
|
|---|
| 621 | (Meta-Object Compiler). Classes that wish to take advantage of the
|
|---|
| 622 | meta-object facilities are either subclasses of QObject, or use the
|
|---|
| 623 | \c{Q_OBJECT} macro. Qt has used this approach for many years and it has
|
|---|
| 624 | proven to be solid and reliable. QtScript uses this meta-object
|
|---|
| 625 | technology to provide scripters with dynamic access to C++ classes
|
|---|
| 626 | and objects.
|
|---|
| 627 |
|
|---|
| 628 | To completely understand how to make C++ objects available to Qt
|
|---|
| 629 | Script, some basic knowledge of the Qt meta-object system is very
|
|---|
| 630 | helpful. We recommend that you read the \l{Qt Object Model}. The
|
|---|
| 631 | information in this document and the documents it links to are very
|
|---|
| 632 | useful for understanding how to implement application objects.
|
|---|
| 633 |
|
|---|
| 634 | However, this knowledge is not essential in the simplest cases.
|
|---|
| 635 | To make an object available in QtScript, it must derive from
|
|---|
| 636 | QObject. All classes which derive from QObject can be introspected
|
|---|
| 637 | and can provide the information needed by the scripting engine at
|
|---|
| 638 | run-time; e.g., class name, functions, signatures. Because we obtain
|
|---|
| 639 | the information we need about classes dynamically at run-time, there
|
|---|
| 640 | is no need to write wrappers for QObject derived classes.
|
|---|
| 641 |
|
|---|
| 642 | \section2 Making C++ Class Member Functions Available in QtScript
|
|---|
| 643 |
|
|---|
| 644 | The meta-object system also makes information about signals and slots
|
|---|
| 645 | dynamically available at run-time. By default, for QObject subclasses,
|
|---|
| 646 | only the signals and slots are automatically made available to scripts.
|
|---|
| 647 | This is very convenient because, in practice, we normally only want to
|
|---|
| 648 | make specially chosen functions available to scripters. When you create
|
|---|
| 649 | a QObject subclass, make sure that the functions you want to expose to
|
|---|
| 650 | QtScript are public slots.
|
|---|
| 651 |
|
|---|
| 652 | For example, the following class definition enables scripting only for
|
|---|
| 653 | certain functions:
|
|---|
| 654 |
|
|---|
| 655 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 19
|
|---|
| 656 |
|
|---|
| 657 | In the example above, aNonScriptableFunction() is not declared as a
|
|---|
| 658 | slot, so it will not be available in QtScript. The other three
|
|---|
| 659 | functions will automatically be made available in QtScript because
|
|---|
| 660 | they are declared in the \c{public slots} section of the class
|
|---|
| 661 | definition.
|
|---|
| 662 |
|
|---|
| 663 | It is possible to make any function script-invokable by specifying
|
|---|
| 664 | the \c{Q_INVOKABLE} modifier when declaring the function:
|
|---|
| 665 |
|
|---|
| 666 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 20
|
|---|
| 667 |
|
|---|
| 668 | Once declared with \c{Q_INVOKABLE}, the method can be invoked from
|
|---|
| 669 | QtScript code just as if it were a slot. Although such a method is
|
|---|
| 670 | not a slot, you can still specify it as the target function in a
|
|---|
| 671 | call to \c{connect()} in script code; \c{connect()} accepts both
|
|---|
| 672 | native and non-native functions as targets.
|
|---|
| 673 |
|
|---|
| 674 | \section2 Making C++ Class Properties Available in QtScript
|
|---|
| 675 |
|
|---|
| 676 | In the previous example, if we wanted to get or set a property using
|
|---|
| 677 | QtScript we would have to write code like the following:
|
|---|
| 678 |
|
|---|
| 679 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 21
|
|---|
| 680 |
|
|---|
| 681 | Scripting languages often provide a property syntax to modify and
|
|---|
| 682 | retrieve properties (in our case the enabled state) of an
|
|---|
| 683 | object. Many script programmers would want to write the above code
|
|---|
| 684 | like this:
|
|---|
| 685 |
|
|---|
| 686 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 22
|
|---|
| 687 |
|
|---|
| 688 | To make this possible, you must define properties in the C++ QObject
|
|---|
| 689 | subclass. For example, the following \c MyObject class declaration
|
|---|
| 690 | declares a boolean property called \c enabled, which uses the function
|
|---|
| 691 | \c{setEnabled(bool)} as its setter function and \c{isEnabled()} as its
|
|---|
| 692 | getter function:
|
|---|
| 693 |
|
|---|
| 694 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 23
|
|---|
| 695 |
|
|---|
| 696 | The only difference from the original code is the use of the macro
|
|---|
| 697 | \c{Q_PROPERTY}, which takes the type and name of the property, and
|
|---|
| 698 | the names of the setter and getter functions as arguments.
|
|---|
| 699 |
|
|---|
| 700 | If you don't want a property of your class to be accessible in
|
|---|
| 701 | QtScript, you set the \c{SCRIPTABLE} attribute to \c false when
|
|---|
| 702 | declaring the property; by default, the \c{SCRIPTABLE} attribute is
|
|---|
| 703 | \c true. For example:
|
|---|
| 704 |
|
|---|
| 705 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 24
|
|---|
| 706 |
|
|---|
| 707 | \section2 Reacting to C++ Objects Signals in Scripts
|
|---|
| 708 |
|
|---|
| 709 | In the Qt object model, signals are used as a notification mechanism
|
|---|
| 710 | between QObjects. This means one object can connect a signal to
|
|---|
| 711 | another object's slot and, every time the signal is emitted, the slot
|
|---|
| 712 | is called. This connection is established using the QObject::connect()
|
|---|
| 713 | function.
|
|---|
| 714 |
|
|---|
| 715 | The signals and slots mechanism is also available to QtScript
|
|---|
| 716 | programmers. The code to declare a signal in C++ is the same,
|
|---|
| 717 | regardless of whether the signal will be connected to a slot in C++
|
|---|
| 718 | or in QtScript.
|
|---|
| 719 |
|
|---|
| 720 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 25
|
|---|
| 721 |
|
|---|
| 722 | The only change we have made to the code in the previous section is
|
|---|
| 723 | to declare a signals section with the relevant signal. Now, the
|
|---|
| 724 | script writer can define a function and connect to the object like
|
|---|
| 725 | this:
|
|---|
| 726 |
|
|---|
| 727 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 26
|
|---|
| 728 |
|
|---|
| 729 | \section2 Design of Application Objects
|
|---|
| 730 |
|
|---|
| 731 | The previous section described how to implement C++ objects which
|
|---|
| 732 | can be used in QtScript. Application objects are the same kind of
|
|---|
| 733 | objects, and they make your application's functionality available to
|
|---|
| 734 | QtScript scripters. Since the C++ application is already written
|
|---|
| 735 | in Qt, many objects are already QObjects. The easiest approach would
|
|---|
| 736 | be to simply add all these QObjects as application objects to the
|
|---|
| 737 | scripting engine. For small applications this might be sufficient,
|
|---|
| 738 | but for larger applications this is probably not the right
|
|---|
| 739 | approach. The problem is that this method reveals too much of the
|
|---|
| 740 | internal API and gives script programmers access to application
|
|---|
| 741 | internals which should not be exposed.
|
|---|
| 742 |
|
|---|
| 743 | Generally, the best way of making application functionality available
|
|---|
| 744 | to scripters is to code some QObjects which define the applications
|
|---|
| 745 | public API using signals, slots, and properties. This gives you
|
|---|
| 746 | complete control of the functionality made available by the
|
|---|
| 747 | application. The implementations of these objects simply call the
|
|---|
| 748 | functions in the application which do the real work. So, instead of
|
|---|
| 749 | making all your QObjects available to the scripting engine, just add
|
|---|
| 750 | the wrapper QObjects.
|
|---|
| 751 |
|
|---|
| 752 | \section3 Returning QObject Pointers
|
|---|
| 753 |
|
|---|
| 754 | If you have a slot that returns a QObject pointer, you should note
|
|---|
| 755 | that, by default, Qt Script only handles conversion of the types
|
|---|
| 756 | QObject* and QWidget*. This means that if your slot is declared
|
|---|
| 757 | with a signature like "MyObject* getMyObject()", QtScript doesn't
|
|---|
| 758 | automatically know that MyObject* should be handled in the same way
|
|---|
| 759 | as QObject* and QWidget*. The simplest way to solve this is to only
|
|---|
| 760 | use QObject* and QWidget* in the method signatures of your scripting
|
|---|
| 761 | interface.
|
|---|
| 762 |
|
|---|
| 763 | Alternatively, you can register conversion functions for your custom
|
|---|
| 764 | type with the qScriptRegisterMetaType() function. In this way, you
|
|---|
| 765 | can preserve the precise typing in your C++ declarations, while
|
|---|
| 766 | still allowing pointers to your custom objects to flow seamlessly
|
|---|
| 767 | between C++ and scripts. Example:
|
|---|
| 768 |
|
|---|
| 769 | \snippet doc/src/snippets/code/doc_src_qtscript.qdoc 43
|
|---|
| 770 |
|
|---|
| 771 | \section1 Function Objects and Native Functions
|
|---|
| 772 |
|
|---|
| 773 | In Qt Script, functions are first-class values; they are objects that
|
|---|
|
|---|