Singleton object which was provided as a model for a table view in qml destroyed during closing qml panel
-
Hi,
I have a class named FilterData inherited from QSortFilterProxyModel in C++ for filtering and sorting of a table.
Created a singleton from FilterData for accessing it in qml.
qmlRegisterSingletonType<FilterData >(uri, 1, 0, ensureUpperCaseLetter.toLatin1().data(), FilterData::getInstance);In MyView.qml, I created a table view and provided this singleton as a model to the view
TableView { anchors.fill: idTableRect model: TableViewSingleton delegate: Rectangle {...} ... ... }
I have a list of qmls and loading them using panelLoader. When I load MyView.qml for the first time, the view loads perfectly. All sorting and filtering options works as expected.
When I close the view. i.e., removing it from the panel loader, the destructor of FilterData is being called and the singleton object is destroyed.
Can't able to figure out the reason for singleton destruction as no explicit call was made to destroy. For other panels which I unload, I see no destructor is being called. Issue occurs only with this qml.
When I load MyView.qml for the next time, the program crashes as no singleton object is associated to "TableViewSingleton"
Can anyone help me on this?
Note: I see this issue occurs only with 5.14.2 and not with 5.12.12
-
@darthana you need to set the object to Cpp ownership:
NOTE: A QObject singleton type instance returned from a singleton type provider is owned by the QML engine unless the object has explicit QQmlEngine::CppOwnership flag set.
For example:
QJSEngine::setObjectOwnership(instance, QJSEngine::CppOwnership);
-
-
@darthana said in Singleton object which was provided as a model for a table view in qml destroyed during closing qml panel:
@sierdzio But if the object is created in C++ and shared to JavaScript, by default the memory management of the QObject remains in C++. Do we need to explicitly set object ownership?
Yes you need to set it explicitly. The documentation clearly states: a singleton type provider is owned by the QML engine.
Also I dont find setObjectOwnership() under QJSEngine.
In Qt 5 it is under QQmlEngine