Solved QVariant pointer data after deletion of QStandarditem (QVariant data set with setData)
-
I have a Question about the destructor of QStandardItems.
I have a class myClass, including the following line at the end of the Class-declaration (header).
Q_DECLARE_METATYPE(myClass*)
In the Construcotr i have used the following line in the Constructor to connect the Item via QVariant-Pointer with the data of the item.
treeViewItem->setData(QVariant::fromValue(this));And here is My Question:
When i Delete the UI, the TreeView and I think the Model and its bacame deleted, too. But delete an item the content of the Q-Variant datapointer in his destructor or do i have to delete the data-content manually? -
hi and welcome
if you new it, then you must delete it yourself.
like myClass *p = new myClass();
and then put it in QVariant.but if myclass is based on widget and you give it
a parent
myClass *p = new myClass(this); // this being a dialog or mainwindow
then you should not delete it as the parent will. -
ok, thanks. is there an possibility to add the destructor-line to the destrubtor of the Item? It would be very difficult to delete all of them manually becouse there are 6 different classes stacked together and they are only connected by the Items (to move them later by drag and drop, but thats another topic). There are other ways to manage it like running a lop which delete the data, but i think that my 1st method with adding the line to the destructor of the Item would be better.
-
hi
I wonder if you could let your classes inherit from QObject and accept the QStandardItems as parent so
that the normal clean up qt does also deletes the custom classes.So this custom class is shared between QStandardItems ?
or is it sort of moved to next container? -
Here are all relevant lines for the linking of the item and myclass
/**MyClass prototype (.h)**/ #include <QStandardItem> #include <QMetaType> class MyClass { ... private: QStandardItem *treeViewItem; QStandardItem *treeViewItemLabel; ... }; Q_DECLARE_METATYPE(McArea*) /**MyClass constructor (.c)**/ MyClass::MyClass(..., QStandardItem *treeViewParent) { ... //linking item with item treeViewItem->setData(QVariant::fromValue(this)); ... treeViewParent->appendRow(QList<QStandardItem*>() << treeViewItem << treeViewItemLabel); ... }
the reason i am asking this question is that the program crashed sometimes after closing it. I fixed it with removing the manual destructor of MyClass. I tested every destructor before implementing the new concept, and every constructor hasnt any mistake and worked fine.
The Problem is, that the ram-usage get later Mb-GB ranges, so i dont want to risk anything with not deleted Items. -
I solved the problem with using another concept. Now, i delete a row via the pointer stored as data and deleting it directly. In the destructor of the Item i clean the TreeViewItems and the content., i think this is an better way than editing the destructor of every QStandardItem.
-
@J0nny
Hi
Yes it does sound better