View Full Version : QtScript: accessing methods of QObject* subclass from script

11th April 2012, 00:11
I am very new to QtScript and I have used QScriptEngine::evaluate() to evaluate this script:

function render(node) {
var stream = node.getStream();
print("typeof stream "+typeof stream + "," + stream);
var test = stream.test();

Now, I have used Q_DECLARE_METATYPE(Stream*)
and node.getStream() returns Stream*

In the script the output is as follows:

typeof stream object,QVariant(Stream*)

Stream has a public slot with testmethod() returning an int:

public slots:
int testmethod() {return 8;}

But then I get the following TypeError in the script when I try to access the testmethod() method of Stream

TypeError: Result of expression 'stream.testmethod' [undefined] is not a function.

I am obviously not doing this right? Can someone help me in the right direction to make this work?

Many thanks.

5th July 2012, 00:09
Bump, same problem.

5th July 2012, 00:27
The object needs to be exposed as QObject and not QVariant.

5th July 2012, 00:58
Ok, mine is not exactly the same problem.

I had exposed a instance of a class that inherits QObject, ClsUserManager:


UserManager has this public slot:

OtherCustomClass* getUser(QString id);

OtherCustomClass has a slot:

void setProperty(QString p)

I tried to call de slot from my script:


I get:

Debug: "TypeError: cannot call getUser(): unknown return type `OtherCustomClass*' (register the type with qScriptRegisterMetaType())"

The problem is that I can't call qScriptRegisterMetaType() from OtherCustomClass, it gets "QScriptEngine * engine" as first param, and I created some instances of QScriptEngine (by demmand) dinamically.


5th July 2012, 08:58
Does "OtherCustomClass" inherit QObject?

5th July 2012, 17:13
Yes, all classes I use in the project inherit QObject.

5th July 2012, 21:02
Does it have a Q_OBJECT macro?

5th July 2012, 21:07
Yes, I can call their slots from C++ code without any problem. I could try to reproduce it on a minimal compilable project if you need it.

5th July 2012, 22:22
Ok, got it...

The problem is that you need to teach your script engine to convert your objects to script values.

Here is the relevant code:


QScriptValue toScriptValue(QScriptEngine *e, OtherCustomClass* const &o) {
return e->newQObject(o);

void fromScriptValue(const QScriptValue &val, OtherCustomClass* &o) {
o = qobject_cast<OtherCustomClass*>(val.toQObject());

qScriptRegisterMetaType<OtherCustomClass*>(engine, fromScriptValue, toScriptValue);

Or more general:

template <typename Tp>
QScriptValue qScriptValueFromQObject(QScriptEngine *engine, Tp const
return engine->newQObject(qobject);

template <typename Tp>
void qScriptValueToQObject(const QScriptValue &value, Tp &qobject)
qobject = qobject_cast<Tp>(value.toQObject());

template <typename Tp>
int qScriptRegisterQObjectMetaType(
QScriptEngine *engine,
const QScriptValue &prototype = QScriptValue()
#ifndef qdoc
, Tp * /* dummy */ = 0
return qScriptRegisterMetaType<Tp>(engine, qScriptValueFromQObject, qScriptValueToQObject, prototype);


The last snippet is not my invention, borrowed it from here: http://lists.trolltech.com/qt-interest/2007-12/thread00158-0.html

5th July 2012, 22:48
Solved, Thank you very much! :D