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

QSObjectFactory Class Reference

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

#include <qsobjectfactory.h>

List of all member functions.

Public Members


Detailed Description

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

If we want script programmers to create their own C++ QObjects we can either provide a QObject subclass that has a slot which is a factory function that returns QObjects, or we can subclass QSObjectFactory and reimplement the create() and classes() functions.

The classes() function is called by the scripting engine to see which classes can be construced by the script programmer. And the create() function is called by the scripting engine to create the instance, i.e. when the user writes something like this:

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

A single QSObjectFactory subclass may be used to provide any number of creatable QObject classes. To make these classes known to the scripting engine, create an instance of the subclass with a pointer to the interpreter as an argument.


Member Function Documentation

QSObjectFactory::QSObjectFactory ( QSInterpreter * i = 0 )

Constructor. To make a QSObjectFactory known to the scripting engine, instantiate a QSObjectFactory instance passing the interpreter i as an argument. The scripting engine will then call the QSObjectFactory's classes() function to obtain the list of classes that this QSObjectFactory is capable of instantiating.

QStringList QSObjectFactory::classes () const [pure virtual]

This pure virtual function should be reimplemented in your QSObjectFactory subclass. It should return a list of the names of all the classes that your QSObjectFactory is capable of instantiating.

This function is used by the scripting engine to find out which classes this QSObjectFactory can instantiate.

QObject * QSObjectFactory::create ( const QString & className, const QValueList<QSArgument> & arguments, QObject * context ) [pure virtual]

This pure virtual function should be reimplemented in your QSObjectFactory subclass. 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 on the arguments. context os 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 like this:

    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 argumenst are invalid or any other invalid operation happens, you can use throwError() to issue a Qt Script error.

void QSObjectFactory::throwError ( const QString & message )

Informs the interpreter than an error occured. 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-2003 Trolltech. All Rights Reserved.


Copyright © 2001-2003 TrolltechTrademarks
QSA version 1.0.0-beta2