[Qt-interest] newQObject ValueOwnership

Kent Hansen khansen at trolltech.com
Mon Mar 8 10:56:27 CET 2010


Hi David,

David Ching wrote:
> Hello, I am confused about QtScript ValueOwnership.  If the code is
>
> void initScripting(Myclass *myclass)
> {
>     QScriptValue myScriptValue = m_engine.newQObject(myClass);
>     m_engine.globalObject().setProperty("myclass", myScriptValue, 
> QScriptEngine::ScriptOwnership);
> }
>   

Here QScriptEngine::ScriptOwnership is being passed to a function that 
expects a value of type QScriptValue::PropertyFlags.
The ownership type should be passed as the second argument to newQObject().

> First, does ValueOwnership affect myClass or myScriptValue?  The doc is 
> vague whether it affects ownership of the C++ object, or the QScriptValue 
> wrapping that C++ object.  My situation is myClass exists for the lifetime 
> of the app, but myScriptValue needs to exist only for the lifetime of the 
> script engine (m_engine), and I would like myScriptValue to be deleted 
> automatically when m_engine is destroyed.
>   

JS objects, including wrapper objects, are subject to garbage 
collection. Additionally, when the engine is destroyed, all JS objects 
created in that engine will be destroyed as well.
 From your description it sounds like you want to use the default 
ownership (QtOwnership).

Regards,
Kent



More information about the Qt-interest-old mailing list