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:
Q_DECLARE_METATYPE(OtherCustomClass*)
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);
Q_DECLARE_METATYPE(OtherCustomClass*)
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);
To copy to clipboard, switch view to plain text mode
Or more general:
template <typename Tp>
QScriptValue qScriptValueFromQObject(QScriptEngine *engine, Tp const
&qobject)
{
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
#endif
)
{
return qScriptRegisterMetaType<Tp>(engine, qScriptValueFromQObject, qScriptValueToQObject, prototype);
}
qScriptRegisterQObjectMetaType<OtherCustomClass*>(engine);
template <typename Tp>
QScriptValue qScriptValueFromQObject(QScriptEngine *engine, Tp const
&qobject)
{
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
#endif
)
{
return qScriptRegisterMetaType<Tp>(engine, qScriptValueFromQObject, qScriptValueToQObject, prototype);
}
qScriptRegisterQObjectMetaType<OtherCustomClass*>(engine);
To copy to clipboard, switch view to plain text mode
The last snippet is not my invention, borrowed it from here: http://lists.trolltech.com/qt-intere...ad00158-0.html
Bookmarks