The only memory you need to delete here is the new QStandardItemModel.
There may be some confusion here as to what a pointer is. A pointer is just a basic variable, like a int or a float. When you call delete on a pointer, you are not deleting the pointer itself, you are deleting the memory the pointer is pointing to. So you tell me, would you want to delete the model memory here? That would probably be bad for your program.
Look at your qobject_cast - that will tell you what you are getting back, in this case a pointer to a QSortFilterProxyModel. So what you have is this:
//Below is a local pointer pointing to new memory allocated on the heap, when the
//method ends, the printModel pointer will go out of scope and be destroyed - that means
//you will have no more access to the memory you allocated with new.
//This is a memory leak.
//Below you create two local pointers (as above), but instantiate them with pointers to
//memory which has already been allocated. When these pointers fall out of scope, it is
//not a memory leak because you did not allocate any memory here in the first place.
//You still have a pointer to the model memory, which is returned by model().
//Below is a local pointer pointing to new memory allocated on the heap, when the
//method ends, the printModel pointer will go out of scope and be destroyed - that means
//you will have no more access to the memory you allocated with new.
//This is a memory leak.
QStandardItemModel * printModel = new QStandardItemModel;
//Below you create two local pointers (as above), but instantiate them with pointers to
//memory which has already been allocated. When these pointers fall out of scope, it is
//not a memory leak because you did not allocate any memory here in the first place.
//You still have a pointer to the model memory, which is returned by model().
QSortFilterProxyModel * tmMain = qobject_cast<QSortFilterProxyModel*>(cw->mainTable()->model());
QSortFilterProxyModel * tmDetail = qobject_cast<QSortFilterProxyModel*>(cw->detailTable()->model());
To copy to clipboard, switch view to plain text mode
I'd be happy to help with any other questions you have. I know it can be a bit confusing.
Bookmarks