QtQuick Control Q_INVOKABLE returning null
-
Guys, I have a big problem in QtQuick/QML for a while and I´m helpless = (
I´m using Qt 5.2 nightly. I have a QAbstractListModel that is connected to QtQuick Control TreeView. If I click on it in the signal ¨onCurrentRowChanged¨ I call a function of this QAbstractListModel called “Get” to get a QObject*. This object is used to show details in the interface.
It do work great for a while, but suddenly when alternating the rows, it will stop working. QML complains that the returned object is NULL.
But Debuging the c++ “get” function, I can see that it is returning a valid object. Please, someone know what is happening ??? -
QML ownership semantics of Q_INVOKABLE functions is that the QML engine will take ownership of the returned pointer, and delete the object during garbage collection if no javascript references to the object exist.
To keep it alive, either manually store a reference to the returned object, or modify the ownership of the object to CppOwnership manually.
Cheers,
Chris. -
Chris,
You solve it !!Thanks a lot !!!
-
Replying to share my personal experience with this issue.
Changing the ownership of the instantiated object to "QQmlEngine::CppOwnership":http://doc.qt.io/qt-5/qqmlengine.html#ObjectOwnership-enum before returning it indeed fixes the issue.
What is not very intuitive though is the fact that you have to change the ownership to CppOwnership even if the objectOwnership() method already returns CppOwnership. This gives something like:
@QObject *
MyClass::InvokableMethod ( ... )
{
QObject * pObject = InstanciateObject( ... );QQmlEngine::ObjectOwnership ow = engine->objectOwnership( pObject );
// Here, ow == QQmlEngine::CppOwnershipengine->setObjectOwnership( pObject, QQmlEngine::CppOwnership );
return pObject;
}@The "setObjectOwnership()":http://doc.qt.io/qt-5/qqmlengine.html#setObjectOwnership and "objectOwnership()":http://doc.qt.io/qt-5/qqmlengine.html#objectOwnership methods seem to perform more stuff than just set and get a two-value property on the object, something that does not appear in the documentation.
Anyway, thanks for the post.
-
Replying to share my personal experience with this issue.
Changing the ownership of the instantiated object to "QQmlEngine::CppOwnership":http://doc.qt.io/qt-5/qqmlengine.html#ObjectOwnership-enum before returning it indeed fixes the issue.
What is not very intuitive though is the fact that you have to change the ownership to CppOwnership even if the objectOwnership() method already returns CppOwnership. This gives something like:
@QObject *
MyClass::InvokableMethod ( ... )
{
QObject * pObject = InstanciateObject( ... );QQmlEngine::ObjectOwnership ow = engine->objectOwnership( pObject );
// Here, ow == QQmlEngine::CppOwnershipengine->setObjectOwnership( pObject, QQmlEngine::CppOwnership );
return pObject;
}@The "setObjectOwnership()":http://doc.qt.io/qt-5/qqmlengine.html#setObjectOwnership and "objectOwnership()":http://doc.qt.io/qt-5/qqmlengine.html#objectOwnership methods seem to perform more stuff than just set and get a two-value property on the object, something that does not appear in the documentation.
Anyway, thanks for the post.