View Full Version : Testing app with Qt Script

Angelo Moriconi
22nd October 2007, 14:44
Dear all,
I would like to use QtScript in order to test a data model for an application, try all the methods, and the behaviour of the GUI during the modification of the data model.

To do this, I would like to create a script console in which the developer could create and use some QObject child (classes that represents the data model) using script.

I read the documentation and I understand well that I can use any instance of data model classes with this method:

void ScriptConsole::addQObjectInstance(QObject *object, const QString &name)
QScriptValue objectValue = m_engine.newQObject(object);
m_engine.globalObject().setProperty(name, objectValue);

How can I use some classes of the data model inside the script ?
I need to define, in some way, the possibility to create new objects of particular classes (that inherit QObject) in the script.

Thanks in advance,


22nd October 2007, 15:26
In your script you can create objects using new.
function xxx()
var obj = new MyObject();
// connect a script function to the signal
obj.enabled = true;
print( "obj is enabled: " + obj.enabled );

Angelo Moriconi
22nd October 2007, 15:56
I know that I can use new, but it doesn't worked as expected.

Suppose that I have a class called Scene, that contains a QList of another class called Panel.
Both Scene and Panel inherith QObject and have all their public methods available as public slots.

If I write:

var s = new Scene;

I receive the following error:

ReferenceError: Scene is not defined

I think it is normal since in the global script environment I didn't define any custom type for Panel and Scene classes.

After creating a new Scene object I would like to use its methods and, if it is possible, add or remove (in the Scene container) new Panel objects created using the script.

Someone has tried to use Qt Script to create and "manage" C++ Objects ?


Angelo Moriconi
22nd October 2007, 16:10
I have found the solution:


Best regards,


22nd October 2007, 16:12
Well, this is what is written in the docs for the QtScript module:

Making a C++ object available to Scripts Written in Qt Script

Making C++ classes and objects available to a scripting language is not trivial because scripting languages tend to be more dynamic than C++, and it must be possible to introspect objects (query information such as function names, function signatures, properties, etc., at run-time). Standard C++ does not provide features for this.
We can achieve the functionality we want by extending C++, using C++'s own facilities so our code is still standard C++. The Qt meta-object system provides the necessary additional functionality. It allows us to write using an extended C++ syntax, but converts this into standard C++ using a small utility program called moc (Meta-Object Compiler). Classes that wish to take advantage of the meta-object facilities are either subclasses of QObject, or use the Q_OBJECT macro. Qt has used this approach for many years and it has proven to be solid and reliable. Qt Script uses this meta-object technology to provide scripters with dynamic access to C++ classes and objects.
To completely understand how to make C++ objects available to Qt Script, some basic knowledge of the Qt meta-object system is very helpful. We recommend that you read the Qt Object Model. The information in this document and the documents it links to are very useful for understanding how to implement application objects.
However, this knowledge is not essential in the simplest cases. To make an object available in Qt Script, it must derive from QObject. All classes which derive from QObject can be introspected and can provide the information needed by the scripting engine at run-time; e.g., class name, functions, signatures. Because we obtain the information we need about classes dynamically at run-time, there is no need to write wrappers for QObject derived classes.

I have never used QtScript myself, but I think that the worst case scenario is that you'll need to access the objects meta-object and marshall the item through it, although from what I see in the above mentioned docs, you shouldn't need to do that.

Angelo Moriconi
23rd October 2007, 09:20
I have another problem :(

In the Scene class to retrieve a Panel from the QList (which is private) I create a getter method that return a pointer to a Panel:

Panel *getPanel(int index);

If I use this method in the script the QScriptValue returned doesn't have the right type.
The return value is a variant.

How can I convert this variant (variant(void*, ...)) and change its type to Panel (in the script) ?