CopperSpice API
1.9.2
|
The QScriptable class provides access to the CsScript environment from member functions. More...
Public Methods | |
QScriptValue | argument (int index) const |
int | argumentCount () const |
QScriptContext * | context () const |
QScriptEngine * | engine () const |
QScriptValue | thisObject () const |
The QScriptable class provides access to the CsScript environment from member functions.
With QScriptEngine::newQObject(), you can expose the signals and slots and properties of any QObject (or subclass) to script code. QScriptable augments this functionality by giving your members access to the CsScript environment they are invoked in; conceptually, it is similar to QObject::sender().
By subclassing QScriptable, you get the following functions in your class: thisObject(), argumentCount(), argument(), context() and engine(). With these functions, you have full access to the CsScript environment from the slots and property access functions of your class, when they are invoked from script code.
For example, you can throw a CsScript exception from a slot; manipulate the ‘this’ object associated with the function call; inspect the arguments stored in the QScriptContext to know the "real" arguments passed to the function from script code; and call script functions from your slot.
A typical use case of QScriptable is to implement prototype objects for custom types. You define the scriptable interface of your custom type in a QScriptable subclass using properties and slots; then you wrap an instance of your class using QScriptEngine::newQObject(), and finally pass the result to QScriptEngine::setDefaultPrototype().
The following is what subclassing QScriptable typically looks like the following.
The only difference from regular QObject subclassing is that you also inherit from QScriptable. In the implementation of your slots, you can then use the functions inherited from QScriptable.
QScriptValue QScriptable::argument | ( | int | index | ) | const |
Returns the function argument at the given index. Returns an invalid QScriptValue if no function call is currently running.
int QScriptable::argumentCount | ( | ) | const |
Returns the number of arguments passed to the function. Returns -1 if no function call is currently running.
QScriptContext * QScriptable::context | ( | ) | const |
Returns a pointer to the QScriptContext associated with the current CsScript function call. Returns a nullptr if no function call is currently running.
QScriptEngine * QScriptable::engine | ( | ) | const |
Returns a pointer to the QScriptEngine associated with the current CsScript function call. Returns a nullptr if no function call is currently running.
QScriptValue QScriptable::thisObject | ( | ) | const |
Returns the object associated with the current CsScript function call. Returns an invalid QScriptValue if no function call is currently running.