Search code examples
c++qtqjsengine

QJSEngine deletes my QObject, how to change ownership after QJSEngine::newQObject?


Trying to perform small script with subclassed QObject as parameter.

QJSEngine jsEngine;
QJSValue arg = jsEngine.newQObject(child); // Child it's subclassed QObject
QJSValue function = jsEngine.evaluate(m_childRestriction);

QJSValue result = function.call(QJSValueList() << arg);

On destroying jsEngine, it calls delete for my child object (as newQObject creates it with JavaScriptOwnership). How to avoid it, how to change ownership for arg?

Script is simple:

function(device) { 
    return device.m_place >=0 && device.m_place < 16; 
}

UPD: It is possible to call QQmlEngine::setObjectOwnership(child, QQmlEngine::CppOwnership); for the object as it is the static function. It just not clear from the help. Think it is suppose to be in QJSEngine also.


Solution

  • It is possible to call QQmlEngine::setObjectOwnership(child, QQmlEngine::CppOwnership); for the object as it is the static function. It just not clear from the help. Think it is suppose to be in QJSEngine also.