Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

qt

qjsengine

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.

like image 953
Serhiy Avatar asked Oct 20 '22 00:10

Serhiy


1 Answers

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.

like image 123
Serhiy Avatar answered Nov 15 '22 06:11

Serhiy