5

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Serhiy
  • 1,332
  • 1
  • 16
  • 24
  • 5
    You might try adding `QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);` to the constructor for your object. Not sure if it applies to your case, but it does prevent Qml from deleting our objects. – Retired Ninja Oct 03 '14 at 11:11
  • I don't have QQmlEngine, it is inherits QJSEngine. – Serhiy Oct 03 '14 at 11:23
  • 1
    You still can use the method that @RetiredNinja mentioned. It's in the same Qt module (so you will always have it available) and it's even used internally by `QJSEngine`! It's actually used by `QJSEngine` to set the ownership of the object to JavaScript. You can call it afterwards like @RetiredNinja said to make it owned back by CPP. – Googie Oct 03 '14 at 13:28
  • Oh yes, It is a static function... Thanks a lot. – Serhiy Oct 03 '14 at 15:07

1 Answers1

4

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.

Serhiy
  • 1,332
  • 1
  • 16
  • 24