|Home | Tutorial | Classes | Functions | QSA Workbench | Language | Qt API | QSA Articles | Qt Script for Applications | ![]() |
The QSObjectFactory class provides a method for Qt Script programs to create C++ QObjects. More...
#include <qsobjectfactory.h>
Inherited by QSInputDialogFactory and QSUtilFactory.
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.
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.
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.
This file is part of Qt Script for Applications, copyright © 2001-2004 Trolltech. All Rights Reserved.
Copyright © 2001-2006 Trolltech | Trademarks | QSA version 1.1.5
|