0

I'm using QJSEngine and binding some objects with engine.globalObject().setProperty(name, engine.newQObject(obj));

The problem is that QJSEngine destructor is calling the destructors of those objects but I want them to outlive the QJSEngine object.

This happens even if I remove the property from globalObject().

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
MattBas
  • 156
  • 2
  • 7

1 Answers1

0

Looks like using QQmlEngine instead of QJSEngine gives access to setObjectOwnership which allows to work around the issue.

https://doc.qt.io/qt-5/qqmlengine.html#setObjectOwnership

From what I understand from the docs though, it will still be an issue with objects returned from Q_INVOKABLE methods, which I'm not sure how to retain ownership of. So if anyone has some ideas, I'd like to know them.

MattBas
  • 156
  • 2
  • 7
  • 1
    This is actually not an issue for objects returned from Q_INVOKABLEs, the QML engine won't take ownership of them if you called `setObjectOwnership` with `CppOwnership` or if they have a parent QObject. – GrecKo Jul 25 '20 at 07:42
  • `QQmlEngine::setObjectOwnership(QObject *object, QQmlEngine::ObjectOwnership ownership)` is static. Thus, it should also work with QJSEngine objects as well – Pcgomes Sep 03 '20 at 20:29