PDA

View Full Version : Qt script return pointer



bunjee
28th December 2008, 01:34
Hey there,

I'm trying to achieve the following using QtScript:


test.prototype.createPage = function(factory)
{
test = factory.newPicture("box.png");
}

The newPicture interface is correctly binded and returns a qkPicture *.
The problem is, due to non typed variables, QtScript assumes he has to copy to content of the qkPicture.

Is there a way to emulate pointer behaviour and dereference test ?
Or a way to bypass non typed scripting syntax ?

Thanks.
Benjamin Arnaud.

bunjee
30th December 2008, 21:54
Returning QObject Pointers

If you have a slot that returns a QObject pointer, you should note that, by default, Qt Script only handles conversion of the types QObject* and QWidget*. This means that if your slot is declared with a signature like "MyObject* getMyObject()", QtScript doesn't automatically know that MyObject* should be handled in the same way as QObject* and QWidget*. The simplest way to solve this is to only use QObject* and QWidget* in the method signatures of your scripting interface.

Alternatively, you can register conversion functions for your custom type with the qScriptRegisterMetaType() function. In this way, you can preserve the precise typing in your C++ declarations, while still allowing pointers to your custom objects to flow seamlessly between C++ and scripts. Example:

class MyObject : public QObject
{
Q_OBJECT
...
};

Q_DECLARE_METATYPE(MyObject*)

QScriptValue myObjectToScriptValue(QScriptEngine *engine, MyObject* const &in)
{ return engine->newQObject(in); }

void myObjectFromScriptValue(const QScriptValue &object, MyObject* &out)
{ out = qobject_cast<MyObject*>(object.toQObject()); }

...

qScriptRegisterMetaType(&engine, myObjectToScriptValue, myObjectFromScriptValue);

http://doc.trolltech.com/main-snapshot/qtscript.html#making-use-of-prototype-based-inheritance