|Home | Tutorial | Classes | Functions | QSA Workbench | Language | Qt API | QSA Articles | Qt Script for Applications |

# QSObjectFactory Class Reference

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

`#include <qsobjectfactory.h>`

Inherited by QSInputDialogFactory and QSUtilFactory.

List of all member functions.

## Public Members

- QSObjectFactory ()
- virtual QObject * create ( const QString & className, const QSArgumentList & arguments, QObject * context )
- void registerClass ( const QString & className, QObject * staticDescriptor = 0 )
- void registerClass ( const QString & className, const QString & cppClassName, QObject * staticDescriptor = 0 )
- void throwError ( const QString & message )

## Protected Members

- 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 and reimplement the create() and classes() functions.

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 & className, const QSArgumentList & 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 QSArgument 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

The classes that a particular QSObjectFactory instance is capable of instantiating is returned by classes().

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 QString & cppClassName, 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. cppClassName 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" );

Providing QString::null in cppClassName 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.

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

### 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.

This file is part of Qt Script for Applications, copyright © 2001-2004 Trolltech. All Rights Reserved.