|  | Home · Tutorial · Classes · Functions · Language · QSA Workbench · Qt Documentation · www.trolltech.com |

# QSObjectFactory Class Reference

The QSObjectFactory class provides a method for Qt Script programs to create C++ QObjects. More...

        #include <QSObjectFactory>

Inherited by QSInputDialogFactory and QSUtilFactory.

- List of all members, including inherited members

### Public Functions

- QSObjectFactory ()
- virtual QObject * create ( const QString & name, const QVariantList & arguments, QObject * context )
- void registerClass ( const QString & className, const QMetaObject * metaObject, QObject * staticDescriptor = 0 )
- void throwError ( const QString & message )

### Protected Functions

- QSInterpreter * interpreter () const

## Detailed Description

The QSObjectFactory class provides a method for Qt Script programs to create C++ QObjects.

To enable script programmers to create their own C++ QObjects, application programmers can provide a QObject subclass that has a slot which is a factory function that returns QObjects. Alternatively, the application programmer can subclass QSObjectFactory, reimplement the create() function and register the provided classes using the registerClass() function.

The registerClass() function is called to register to the scripting engine each of the classes that this object factory can instantiate. In addition, the create() function is called by the scripting engine to create the instance, i.e. when the user writes something like

        var x = new SomeCppObject( arg1, arg2 ); // Qt Script

The function create() is reimplemented in each QSObjectFactory to provide the an instance of the QObject subclass that describes what the scripter should experience as an object of the type SomeCppObject.

A single QSObjectFactory subclass may be used to provide any number of creatable QObject classes. A QSObjectFactory becomes active when it is added to a QSInterpreter using the function QSInterpreter::addObjectFactory(). An object factory can only be added to one QSInterpreter at a time.

## Member Function Documentation

### QSObjectFactory::QSObjectFactory ()

Constructor. Creates the object factory. To make the object factory available to an interpreter, use the function: QSInterpreter::addObjectFactory().

### QObject * QSObjectFactory::create ( const QString & name, const QVariantList & arguments, QObject * context ) `[virtual]`

This virtual function should be reimplemented in your QSObjectFactory subclass when you want to instantiate objects from script. The subclass can be used to create any number of different classes. The name of the required class is passed in the className argument, and the arguments to be passed to that class's constructor are passed in the arguments list. See QVariant for further information about the arguments. context is the script QObject context in which the class has been instantiated, or 0 if the class has not been instantiated in a QObject context.

Only QObject subclasses may be created in this way. This function returns an instance of the requested class.

This function is called by the scripting engine, e.g. when it encounters code similar to the following:

        var x = new ACppObject( arg1, arg2 ); // Qt Script

If the arguments are invalid or any other invalid operation happens, you can use throwError() to issue a Qt Script error.

All objects created from this function are deleted by the engine when the engine registers it as not being referenced anymore, unless the object at this time has a parent, which will then be responsible for ownership.

### QSInterpreter * QSObjectFactory::interpreter () const `[protected]`

Returns the interpreter that this object factory is creating objects for.

### void QSObjectFactory::registerClass ( const QString & className, const QMetaObject * metaObject, QObject * staticDescriptor = 0 )

Registers that this object factory provides the class className.

This function is called once for each of the script classes that a subclass of QSObjectFactory wishes to expose to the scripting engine.

The name className is the name that the class has within the scripting engine. metaObject is used by the interpreter to describe QObjects of returned for className when using syntax completion.

is the name of the C++ class that is actually instantiated when className is passed to the create() function. For instance:

        // in script
        var x = new MyObject();

        // in C++
        QObject *create( const QString &name, ... ) {
            if( name == "MyObject" )
                return new MyScriptObject()
        }

Here we would call registerClass as follows:

        registerClass( "MyObject", &MyScriptObject::staticMetaObject );

Providing a null-pointer for metaObject will tell the interpreter that the class is not instantiable.

The staticDescriptor is used to describe the static part of a class. The properties, signals and slots of the staticDescriptor object will be accessible as className 's static part.

For instance, if you want to expose the entire Qt namespace to QSA, it can be done with the following code:

        class QtNamespace: public QObject
        {
          public:
              const QMetaObject *metaObject()
              {
                  return &QObject::staticQtMetaObject;
              }
        };

        class QtNamespaceProvider: public QSObjectFactory
        {
          public:
              QtNamespaceProvider()
              {
                  registerClass("Qt", 0, new QtNamespace());
              }
        };

When QtNamespaceProvider is registered with an interpreter, scripts in the interpreter can access the Qt namespace through the class name "Qt". (Note that the QtNamespace class cannot have a Q_OBJECT macro, as this macro also declares the metaObject() method.)

### void QSObjectFactory::throwError ( const QString & message )

Informs the interpreter that an error has occurred. The error is treated like a normal Qt Script error. The error message is passed in message.