Correct way to manage memory in Qt when representing a QObject class in qml?

I use C ++ to code most of my programming logic. I like to expose C ++ classes in QML so that QML can use JS to do many complex tasks. However, there is a problem:

If a C ++ function returns a QObject*

Q_INVOKABLE QObject* parseJson(const QString& someArguments)
{
    return new SomeClassExtendsQObject(someArguments);
}

which is then assigned to a variable JSand used

var result = exposingCppObj.parseJson("I'm Arguments");
result.someMemberFunction(...);
.....

if i use

delete result

in QML JS, is the instance really freed, as it would be in C ++?

+4
source share
1 answer

, ++, QML, : QQmlEngine::CppOwnership QQmlEngine::JavaScriptOwnership. , Qt . , QML-, QQmlEngine::JavaScriptOwnership.

QQmlEngine::JavaScriptOwnership, JS , ; .

, QML, destroy(). JS delete , .

+5

Source: https://habr.com/ru/post/1599034/


All Articles