CopperSpice API  1.7.2
QScriptable Class Reference

The QScriptable class provides access to the CsScript environment from member functions. More...

Public Methods

QScriptValue argument (int index) const
 
int argumentCount () const
 
QScriptContextcontext () const
 
QScriptEngineengine () const
 
QScriptValue thisObject () const
 

Detailed Description

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:

class MyScriptableObject : public QObject, protected QScriptable
{
CS_OBJECT(MyScriptableObject)
public:
CS_SLOT_1(Public, void doSomething())
CS_SLOT_2(doSomething)
CS_SLOT_1(Public, double doSomethingElse())
CS_SLOT_2(doSomethingElse)
}

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:

void MyScriptableObject::doSomething()
{
context()->throwError("Threw an error from a slot");
}
double MyScriptableObject::doSomethingElse()
{
return qscriptvalue_cast<double>(thisObject());
}
See also
QScriptEngine::newFunction()

Method Documentation

QScriptValue QScriptable::argument ( int  index) const

Returns the function argument at the given index, or an invalid QScriptValue if the CopperSpice function was not invoked from script code.

See also
argumentCount()
int QScriptable::argumentCount ( ) const

Returns the number of arguments passed to the function in this invocation, or -1 if the CopperSpice function was not invoked from script code.

See also
argument()
QScriptContext * QScriptable::context ( ) const

Returns a pointer to the QScriptContext associated with the current CopperSpice function call, or 0 if the CopperSpice function was not invoked from script code.

QScriptEngine * QScriptable::engine ( ) const

Returns a pointer to the QScriptEngine associated with the current CopperSpice function call, or 0 if the CopperSpice function was not invoked from script code.

QScriptValue QScriptable::thisObject ( ) const

Returns the ‘this’ object associated with the current CopperSpice function call, or an invalid QScriptValue if the CopperSpice function was not invoked from script code.