Can I retain ownership of QObjects passed to QJSEngine?

266 views Asked by At

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().

1

There are 1 answers

2
MattBas On BEST ANSWER

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.