QtScript ModuleThe QtScript module provides classes for making Qt applications scriptable. More... Classes
Detailed Description
Configuring the Build ProcessApplications that use Qt's Script classes need to be configured to be built against the QtScript module. To include the definitions of the module's classes, use the following directive: #include <QtScript> To link against the module, add this line to your qmake .pro file: QT += script The QtScript module is part of the Qt Desktop Edition and the Qt Open Source Edition. Language OverviewQt Script is based on the ECMAScript scripting language, as defined in standard ECMA-262. Microsoft's JScript, and Netscape's JavaScript are also based on the ECMAScript standard. For an overview of ECMAScript, see the ECMAScript reference. If you are not familiar with the ECMAScript language, there are several existing tutorials and books that cover this subject, such as JavaScript: The Definitive Guide. Existing users of Qt Script for Applications (QSA) may find the Moving from QSA to Qt Script document useful when porting QSA scripts to Qt Script. Basic UsageTo evaluate script code, you create a QScriptEngine and call its evaluate() function, passing the script code (text) to evaluate as argument. QScriptEngine engine; qDebug() << "the magic number is:" << engine.evaluate("1 + 2").toNumber(); The return value will be the result of the evaluation (represented as a QScriptValue object); this can be converted to standard C++ and Qt types. Custom properties can be made available to scripts by registering them with the script engine. This is most easily done by setting properties of the script engine's Global Object: QScriptValue val(&engine, 123); engine.globalObject().setProperty("foo", val); qDebug() << "foo times two is:" << engine.evaluate("foo * 2").toNumber(); This places the properties in the script environment, thus making them available to script code. Making a QObject Available to the Script EngineAny QObject subclass can be made available for use with scripts. When a QObject is passed to the QScriptEngine::newQObject() function, a QtScript wrapper object is created that can be used to make the QObject's signals, slots, properties, and child objects available to scripts. Because QtScript uses Qt's meta-object system, there is no need to implement any additional wrappers or bindings. Here's an example of making an instance of a QObject subclass available to script code under the name "myObject": QScriptEngine engine; QObject *someObject = new MyObject; QScriptValue objectValue = engine.newQObject(someObject); engine.globalObject().setProperty("myObject", objectValue); The newQObject() function accepts two additional optional arguments: one is the ownership mode, and the other is a collection of options that allow you to control certain aspects of how the QScriptValue that wraps the QObject should behave. We will come back to the usage of these arguments later. Using Signals and SlotsQtScript uses a different syntax for connecting to and disconnecting from signals than what you're familiar with from C++ (i.e. QObject::connect()). To connect to a signal, you reference the relevant signal as a property of the sender object, and invoke its connect() function. There are three overloads of connect(), each with a corresponding disconnect() overload. The following subsections describe these three forms. Signal to Function Connectionsconnect(function) In this form of connection, the argument to connect() is the function to connect to the signal. function myInterestingScriptFunction() { ... } ... myQObject.somethingChanged.connect(myInterestingScriptFunction); The argument can be a QtScript function, as in the above example, or it can be a QObject slot, as in the following example: myQObject.somethingChanged.connect(myOtherQObject.doSomething); When the argument is a QObject slot, the argument types of the signal and slot do not necessarily have to be compatible; QtScript will, if necessary, perform conversion of the signal arguments to match the argument types of the slot. To disconnect from a signal, you invoke the signal's disconnect() function, passing the function to disconnect as argument: myQObject.somethingChanged.disconnect(myInterestingFunction); myQObject.somethingChanged.disconnect(myOtherQObject.doSomething); When a script function is invoked in response to a signal, the `this' object will be the Global Object. Signal to Member Function Connectionsconnect(thisObject, function) In this form of the connect() function, the first argument is the object that will be bound to the variable, this, when the function specified using the second argument is invoked. If you have a push button in a form, you typically want to do something involving the form in response to the button's clicked signal; passing the form as the this object makes sense in such a case. var obj = { x: 123 }; var fun = function() { print(this.x); }; myQObject.somethingChanged.connect(obj, fun); To disconnect from the signal, pass the same arguments to disconnect(): myQObject.somethingChanged.disconnect(obj, fun); Signal to Named Member Function Connectionsconnect(thisObject, functionName) In this form of the connect() function, the first argument is the object that will be bound to the variable, this, when a function is invoked in response to the signal. The second argument specifies the name of a function that is connected to the signal, and this refers to a member function of the object passed as the first argument (thisObject in the above scheme). Note that the function is resolved when the connection is made, not when the signal is emitted. var obj = { x: 123, fun: function() { print(this.x); } }; myQObject.somethingChanged.connect(obj, "fun"); To disconnect from the signal, pass the same arguments to disconnect(): myQObject.somethingChanged.disconnect(obj, "fun"); Error HandlingWhen connect() or disconnect() succeeds, the function will return undefined; otherwise, it will throw a script exception. You can obtain an error message from the resulting Error object. Example: try { myQObject.somethingChanged.connect(myQObject, "slotThatDoesntExist"); } catch (e) { print(e); } Emitting Signals from ScriptsTo emit a signal from script code, you simply invoke the signal function, passing the relevant arguments: myQObject.somethingChanged("hello"); Overloaded Signals and SlotsWhen a signal or slot is overloaded, QtScript will attempt to pick the right overload based on the actual types of the QScriptValue arguments involved in the function invocation. For example, if your class has slots myOverloadedSlot(int) and myOverloadedSlot(QString), the following script code will behave reasonably: myQObject.myOverloadedSlot(10); // will call the int overload myQObject.myOverloadedSlot("10"); // will call the QString overload You can specify a particular overload by using array-style property access with the normalized signature of the C++ function as the property name: myQObject['myOverloadedSlot(int)']("10"); // call int overload; the argument is converted to an int myQObject['myOverloadedSlot(QString)'](10); // call QString overload; the argument is converted to a string If the overloads have different number of arguments, QtScript will pick the overload with the argument count that best matches the actual number of arguments passed to the slot. For overloaded signals, QtScript will connect to the most general overload, unless you refer to the signal with its full normalized signature. Accessing PropertiesThe properties of the QObject are available as properties of the corresponding Qt Script object. When you manipulate a property in script code, the C++ get/set method for that property will automatically be invoked. For example, if your C++ class has a property declared as follows: Q_PROPERTY(bool enabled READ enabled WRITE setEnabled) then script code can do things like the following: myQObject.enabled = true; ... myQObject.enabled = !myQObject.enabled; Accessing Child QObjectsEvery named child of the QObject is by default available as a property of the corresponding Qt Script object. For example, if you have a QDialog with a child widget whose objectName() is "okButton", you can access this object in script code through the expression myDialog.okButton Since objectName is itself a Q_PROPERTY, you can manipulate the name in script code to, for example, rename an object: myDialog.okButton.objectName = "cancelButton";
// from now on, myDialog.cancelButton references the button
You can also use the functions findChild() and findChildren() to find children. These two functions behave identically to QObject::findChild() and QObject::findChildren(), respectively. For example, we can use these functions to find objects using strings and regular expressions: var okButton = myDialog.findChild("okButton"); if (okButton != null) { // do something with the OK button } var buttons = myDialog.findChildren(RegExp("button[0-9]+")); for (var i = 0; i < buttons.length; ++i) { // do something with buttons[i] } You typically want to use findChild() when manipulating a form that uses nested layouts; that way the script is isolated from the details about which particular layout a widget is located in. Controlling QObject OwnershipBy default, the script engine does not take ownership of the QObject that is passed to QScriptEngine::newQObject(); the object is managed according to Qt's object ownership (see Object Trees and Object Ownership). You can change this behavior by specifying a different ownership mode as the second argument, overriding the default (QScriptEngine::QtOwnership). Specifying QScriptEngine::ScriptOwnership as the ownership mode will cause the script engine to take full ownership of the QObject and delete it when it determines that it is safe to do so. This ownership mode is appropriate if the QObject does not have a parent object, and/or the QObject is created in the context of the script engine and is not intended to outlive the script engine. For example, a constructor function that constructs QObjects only to be used in the script environment is a good candidate: QScriptValue myQObjectConstructor(QScriptContext *context, QScriptEngine *engine)
{
// let the engine manage the new object's lifetime.
return engine->newQObject(new MyQObject(), QScriptEngine::ScriptOwnership);
}
Another ownership mode is QScriptEngine::AutoOwnership, where the ownership is based on whether the QObject has a parent or not. If the QtScript garbage collector finds that the QObject is no longer referenced within the script environment, the QObject will be deleted only if it does not have a parent. Customizing Access to the QObjectQScriptEngine::newQObject() can take a third argument which allows you to control various aspects of the access to the QObject through the QtScript wrapper object it returns. QScriptEngine::ExcludeChildObjects specifies that child objects of the QObject should not appear as properties of the wrapper object. QScriptEngine::ExcludeSuperClassProperties and QScriptEngine::ExcludeSuperClassMethods can be used to avoid exposing members that are inherited from the QObject's superclass. This is useful for defining a "pure" interface where inherited members don't make sense from a scripting perspective; e.g., you don't want script authors to be able to change the objectName property of the object or invoke the deleteLater() slot. QScriptEngine::AutoCreateDynamicProperties specifies that properties that don't already exist in the QObject should be created as dynamic properties of the QObject, rather than as properties of the QtScript wrapper object. If you want new properties to truly become persistent properties of the QObject, rather than properties that are destroyed along with the wrapper object (and that aren't shared if the QObject is wrapped multiple times with newQObject()), you should use this option. Conversion Between QtScript and C++ TypesQtScript will perform type conversion when a value needs to be converted from the script side to the C++ side or vice versa; for instance, when a C++ signal triggers a script function, when you access a QObject property in script code, or when you call QScriptEngine::toScriptValue() or QScriptEngine::fromScriptValue() in C++. QtScript provides default conversion operations for many of the built-in Qt types. You can change the conversion operation for a type by registering your own conversion functions with qScriptRegisterMetaType(). The following table describes the default conversion from a QScriptValue to a C++ type.
Additionally, QtScript will handle the following cases:
The following table describes the default behavior when a QScriptValue is constructed from a C++ type:
Other types will be wrapped using QScriptEngine::newVariant(). For null pointers of any type, the result is QScriptEngine::nullValue(). How to Design and Implement Application ObjectsThis section explains how to implement application objects and provides the necessary technical background material. Making a C++ object available to Scripts Written in Qt ScriptMaking C++ classes and objects available to a scripting language is not trivial because scripting languages tend to be more dynamic than C++, and it must be possible to introspect objects (query information such as function names, function signatures, properties, etc., at run-time). Standard C++ does not provide features for this. We can achieve the functionality we want by extending C++, using C++'s own facilities so our code is still standard C++. The Qt meta-object system provides the necessary additional functionality. It allows us to write using an extended C++ syntax, but converts this into standard C++ using a small utility program called moc (Meta-Object Compiler). Classes that wish to take advantage of the meta-object facilities are either subclasses of QObject, or use the Q_OBJECT macro. Qt has used this approach for many years and it has proven to be solid and reliable. Qt Script uses this meta-object technology to provide scripters with dynamic access to C++ classes and objects. To completely understand how to make C++ objects available to Qt Script, some basic knowledge of the Qt meta-object system is very helpful. We recommend that you read the Qt Object Model. The information in this document and the documents it links to are very useful for understanding how to implement application objects. However, this knowledge is not essential in the simplest cases. To make an object available in Qt Script, it must derive from QObject. All classes which derive from QObject can be introspected and can provide the information needed by the scripting engine at run-time; e.g., class name, functions, signatures. Because we obtain the information we need about classes dynamically at run-time, there is no need to write wrappers for QObject derived classes. Making C++ Class Member Functions Available in Qt ScriptThe meta-object system also makes information about signals and slots dynamically available at run-time. By default, for QObject subclasses, only the signals and slots are automatically made available to scripts. This is very convenient because, in practice, we normally only want to make specially chosen functions available to scripters. When you create a QObject subclass, make sure that the functions you want to expose to Qt Script are public slots. For example, the following class definition enables scripting only for certain functions: class MyObject : public QObject
{
Q_OBJECT
public:
MyObject( ... );
void aNonScriptableFunction();
public slots: // these functions (slots) will be available in Qt Script
void calculate( ... );
void setEnabled( bool enabled );
bool isEnabled() const;
private:
....
};
In the example above, aNonScriptableFunction() is not declared as a slot, so it will not be available in Qt Script. The other three functions will automatically be made available in Qt Script because they are declared in the public slots section of the class definition. It is possible to make any function script-invokable by specifying the Q_INVOKABLE modifier when declaring the function: class MyObject : public QObject { Q_OBJECT public: Q_INVOKABLE void thisMethodIsInvokableInQtScript(); void thisMethodIsNotInvokableInQtScript(); ... }; Once declared with Q_INVOKABLE, the method can be invoked from Qt Script code just as if it were a slot. Although such a method is not a slot, you can still specify it as the target function in a call to connect() in script code; connect() accepts both native and non-native functions as targets. Making C++ Class Properties Available in Qt ScriptIn the previous example, if we wanted to get or set a property using Qt Script we would have to write code like the following: var obj = new MyObject; obj.setEnabled( true ); print( "obj is enabled: " + obj.isEnabled() ); Scripting languages often provide a property syntax to modify and retrieve properties (in our case the enabled state) of an object. Many script programmers would want to write the above code like this: var obj = new MyObject; obj.enabled = true; print( "obj is enabled: " + obj.enabled ); To make this possible, you must define properties in the C++ QObject subclass. For example, the following MyObject class declaration declares a boolean property called enabled, which uses the function setEnabled(bool) as its setter function and isEnabled() as its getter function: class MyObject : public QObject { Q_OBJECT // define the enabled property Q_PROPERTY( bool enabled WRITE setEnabled READ isEnabled ) public: MyObject( ... ); void aNonScriptableFunction(); public slots: // these functions (slots) will be available in Qt Script void calculate( ... ); void setEnabled( bool enabled ); bool isEnabled() const; private: .... }; The only difference from the original code is the use of the macro Q_PROPERTY, which takes the type and name of the property, and the names of the setter and getter functions as arguments. If you don't want a property of your class to be accessible in QtScript, you set the SCRIPTABLE attribute to false when declaring the property; by default, the SCRIPTABLE attribute is true. For example: Q_PROPERTY(int nonScriptableProperty READ foo WRITE bar SCRIPTABLE false) Reacting to C++ Objects Signals in ScriptsIn the Qt object model, signals are used as a notification mechanism between QObjects. This means one object can connect a signal to another object's slot and, every time the signal is emitted, the slot is called. This connection is established using the QObject::connect() function. The signals and slots mechanism is also available to Qt Script programmers. The code to declare a signal in C++ is the same, regardless of whether the signal will be connected to a slot in C++ or in Qt Script. class MyObject : public QObject { Q_OBJECT // define the enabled property Q_PROPERTY( bool enabled WRITE setEnabled READ isEnabled ) public: MyObject( ... ); void aNonScriptableFunction(); public slots: // these functions (slots) will be available in Qt Script void calculate( ... ); void setEnabled( bool enabled ); bool isEnabled() const; signals: // the signals void enabledChanged( bool newState ); private: .... }; The only change we have made to the code in the previous section is to declare a signals section with the relevant signal. Now, the script writer can define a function and connect to the object like this: function enabledChangedHandler( b )
{
print( "state changed to: " + b );
}
function init()
{
var obj = new MyObject();
// connect a script function to the signal
obj["enabledChanged(bool)"].connect(enabledChangedHandler);
obj.enabled = true;
print( "obj is enabled: " + obj.enabled );
}
Design of Application ObjectsThe previous section described how to implement C++ objects which can be used in Qt Script. Application objects are the same kind of objects, and they make your application's functionality available to Qt Script scripters. Since the C++ application is already written in Qt, many objects are already QObjects. The easiest approach would be to simply add all these QObjects as application objects to the scripting engine. For small applications this might be sufficient, but for larger applications this is probably not the right approach. The problem is that this method reveals too much of the internal API and gives script programmers access to application internals which should not be exposed. Generally, the best way of making application functionality available to scripters is to code some QObjects which define the applications public API using signals, slots, and properties. This gives you complete control of the functionality made available by the application. The implementations of these objects simply call the functions in the application which do the real work. So, instead of making all your QObjects available to the scripting engine, just add the wrapper QObjects. Making Use of Prototype-Based InheritanceIn ECMAScript, inheritance is based on the concept of shared prototype objects; this is quite different from the class-based inheritance familiar to C++ programmers. With Qt Script, you can associate a custom prototype object with a C++ type using QScriptEngine::setDefaultPrototype(); this is the key to providing a script interface to that type. Since the QtScript module is built on top of Qt's meta-type system, this can be done for any C++ type. This section explains the underlying concepts of prototype-based inheritance. Once these concepts are understood, the associated practices can be applied throughout the QtScript API in order to create well-behaved, consistent bindings to C++ that will fit nicely into the ECMAScript universe. When experimenting with QtScript objects and inheritance, it can be helpful to use the interactive interpreter included with the QtScript examples, located in examples/script/qscript. Prototype Objects and Shared PropertiesThe purpose of a Qt Script prototype object is to define behavior that should be shared by a set of other QtScript objects. We say that objects which share the same prototype object belong to the same class (again, on the technical side this should not to be confused with the class constructs of languages like C++ and Java; ECMAScript has no such construct). The basic prototype-based inheritance mechanism works as follows: Each QtScript object has an internal link to another object, its prototype. When a property is looked up in an object, and the object itself does not have the property, the property is looked up in the prototype object instead; if the prototype has the property, then that property is returned. Otherwise, the property is looked up in the prototype of the prototype object, and so on; this chain of objects constitutes a prototype chain. The chain of prototype objects is followed until the property is found or the end of the chain is reached. For example, when you create a new object by the expression new Object(), the resulting object will have as its prototype the standard Object prototype, Object.prototype; through this prototype relation, the new object inherits a set of properties, including the hasOwnProperty() function and toString() function: var o = new Object(); o.foo = 123; print(o.hasOwnProperty('foo')); // true print(o.hasOwnProperty('bar')); // false print(o); // calls o.toString(), which returns "[object Object]" The toString() function itself is not defined in o (since we did not assign anything to o.toString), so instead the toString() function in the standard Object prototype is called, which returns a highly generic string representation of o ("[object Object]"). Note that the properties of the prototype object are not copied to the new object; only a link from the new object to the prototype object is maintained. This means that changes done to the prototype object will immediately be reflected in the behavior of all objects that have the modified object as their prototype. Defining Classes in a Prototype-Based UniverseIn Qt Script, a class is not defined explicitly; there is no class keyword. Instead, you define a new class in two steps:
With this arrangement, the constructor's public prototype property will automatically be set as the prototype of objects created by applying the new operator to your constructor function; e.g., the prototype of an object created by new Foo() will be the value of Foo.prototype. Functions that don't operate on the this object ("static" methods) are typically stored as properties of the constructor function, not as properties of the prototype object. The same is true for constants, such as enum values. The following code defines a simple constructor function for a class called Person: function Person(name) { this.name = name; } Next, you want to set up Person.prototype as your prototype object; i.e., define the interface that should be common to all Person objects. Qt Script automatically creates a default prototype object (by the expression new Object()) for every script function; you can add properties to this object, or you can assign your own custom object. (Generally speaking, any Qt Script object can act as prototype for any other object.) Here's an example of how you might want to override the toString() function that Person.prototype inherits from Object.prototype, to give your Person objects a more appropriate string representation: Person.prototype.toString = function() { return "Person(name: " + this.name + ")"; } This resembles the process of reimplementing a virtual function in C++. Henceforth, when the property named toString is looked up in a Person object, it will be resolved in Person.prototype, not in Object.prototype as before: var p1 = new Person("John Doe"); var p2 = new Person("G.I. Jane"); print(p1); // "Person(name: John Doe)" print(p2); // "Person(name: G.I. Jane)" There are also some other interesting things we can learn about a Person object: print(p1.hasOwnProperty('name')); // 'name' is an instance variable, so this returns true print(p1.hasOwnProperty('toString')); // returns false; inherited from prototype print(p1 instanceof Person); // true print(p1 instanceof Object); // true The hasOwnProperty() function is not inherited from Person.prototype, but rather from Object.prototype, which is the prototype of Person.prototype itself; i.e., the prototype chain of Person objects is Person.prototype followed by Object.prototype. This prototype chain establishes a class hierarchy, as demonstrated by applying the instanceof operator; instanceof checks if the value of the public prototype property of the constructor function on the right-hand side is reached by following the prototype chain of the object on the left-hand side. When defining subclasses, there's a general pattern you can use. The following example shows how one can create a subclass of Person called Employee: function Employee(name, salary) { Person.call(this, name); // call base constructor this.salary = salary; } // set the prototype to be an instance of the base class Employee.prototype = new Person(); // initialize prototype Employee.prototype.toString = function() { ... } Again, you can use the instanceof to verify that the class relationship between Employee and Person has been correctly established: var e = new Employee("Johnny Bravo", 5000000); print(e instanceof Employee); // true print(e instanceof Person); // true print(e instanceof Object); // true print(e instanceof Array); // false This shows that the prototype chain of Employee objects is the same as that of Person objects, but with Employee.prototype added to the front of the chain. Prototype-Based Programming with the QtScript C++ APIYou can use QScriptEngine::newFunction() to wrap native functions. When implementing a constructor function, you also pass the prototype object as an argument to QScriptEngine::newFunction(). You can call QScriptValue::construct() to call a constructor function, and you can use QScriptValue::call() from within a native constructor function if you need to call a base class constructor. The QScriptable class provides a convenient way to implement a prototype object in terms of C++ slots and properties. Take a look at the Default Prototypes Example to see how this is done. Alternatively, the prototype functionality can be implemented in terms of standalone native functions that you wrap with QScriptEngine::newFunction() and set as properties of your prototype object by calling QScriptValue::setProperty(). In the implementation of your prototype functions, you use QScriptable::thisObject() (or QScriptContext::thisObject()) to obtain a reference to the QScriptValue being operated upon; then you call qscriptvalue_cast() to cast it to your C++ type, and perform the relevant operations using the usual C++ API for the type. You associate a prototype object with a C++ type by calling QScriptEngine::setDefaultPrototype(). Once this mapping is established, Qt Script will automatically assign the correct prototype when a value of such a type is wrapped in a QScriptValue; either when you explicitly call QScriptEngine::toScriptValue(), or when a value of such a type is returned from a C++ slot and internally passed back to script code by the engine. This means you don't have to implement wrapper classes if you use this approach. ECMAScript CompatibilityQt Script implements all the built-in classes and functions defined in ECMA-262. The Date parsing and string conversion functions are implemented using QDateTime::fromString() and QDateTime::toString(), respectively. The RegExp class is a wrapper around QRegExp. The QRegExp semantics do not precisely match the semantics for regular expressions defined in ECMA-262. Qt Script Extensions to ECMAScript
[Previous: QtSvg Module] [Qt's Modules] [Next: QtXml Module] |
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le Qt Developer Network au hasardCombiner licence, à propos et fermer d'une dernière manièreLe Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.
CommunautéRessources
Liens utilesContact
Qt dans le magazine |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 4.3 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com