HomePage RecentChanges

QScriptNotes

Some notes on QScript

To add a new QObject type to the script

You need to declare it as a metatype.

Q_DECLARE_METATYPE(QPushButton*)

You need to create a static instance of the object that can be used as a prototype.

QScriptValue s_button_proto;

Create a factory method that will create a new QPushButton?.

QScriptValue Button( QScriptContext *context, QScriptEngine *engine )
{
        QWidget *parent = 0;
        QString text;

        if( context->argumentCount() == 1 )
        {
                if( context->argument(0).isString() )
                        text = context->argument(0).toString();
                else if( context->argument(0).isQObject() )
                        parent = qobject_cast<QWidget*>(context->argument(0).toQObject());
        }

        else if( context->argumentCount() == 2 )
        {
                text = context->argument(0).toString();
                parent = qobject_cast<QWidget*>(context->argument(1).toQObject());
        }

        QPushButton *button = new QPushButton(text,parent);


        QScriptValue b = engine->newQObject( button, QScriptEngine::AutoOwnership );
        b.setPrototype( s_button_proto );
        return b;
}