However I did certainly what you said that was only little part.
Thank for your answer, without it I would only find a way around this issue probably. (like 2 parallel QLists)
So what works?
I make additional QObject with parent "this (list of lists)".
Then I make it parent of every QObject I want to delete (QObjects with list and QObjects within lists). I don't change CppOwnership.
After that I clear() list and add new QObjects (obsolete QObjects stay accessible as children of that additional QObject).
After final emit I delete that QObject with deleteLater(). QPointer isn't necessary here I think. I tried deleteLater in another way of doing things and it crashed, so that is the only way I found.
My application deletes QObject and all children with it. With neither crash or leak.
QObject * delQObject = new QObject (this);
set_parents_list(delQObject );
m_list_of_listQobjects.clear();
for ()
m_list_of_listQobjects.append(new listQobject);
emit m_list_of_listQobjectsChanged(m_list_of_listQobjects());
delQObject->deleteLater();
where:
listoflistsofQObjects :: set_parents_list(QObject * parent) {
for (int i = m_list_of_listQobjects.count(); i>0; i--) {
m_list_of_listQobjects[i-1]->setParent(parent);
m_list_of_listQobjects[i-1]->set_parents_list(parent);
}
}
Following function isn't necessary if QObjects already have parent that is deleted (like listofQObjects or some QObject inside it). It's safer to make elements children, because then one doesn't need to call deletes in destructor.
listofQObjects :: set_parents_list(QObject * parent) {
for (int i = m_list_ofQobjects.count(); i>0; i--) {
m_list_ofQobjects[i-1]->setParent(parent);
}
}
I don't know if is it "right way", I even found here somebody uses my earlier version with additional list (but it's unsafe for sure).
The best explanation how to manage memory in qt I found here.
If there is better way to do so, please share.