QVector remove behaviour
-
Hi,
i have a question about the remove function from the QVector.
I have aQVector<SellInfo*>
as member in a class.SellInfo is defined that way:
struct SellInfo { QString name; QSpinBox* box; };
So now i'd like to know how the destructor of my class/Vector will work.
Step 1: i delete the class instance
Step 2: the class's destructor deletes the QVector and the SpinBoxes (because they are children of the class)
Step 3: QVector ???Does QVector delete all the SellInfo structs, or not?? and if the QVector does not, how can i delete them? I would use the remove, or erease function, but will these function only remove the pointer from the vector, or will they delete the objects, the pointer refers to?
And if they delete, will the struct destructor delete the QSpinBox pointer?? if so, they'll be deleted twice and that will cause big problems.
I hope you can help me with that, and i realy hope that the documentation will be updated with these informations
-
QVector's destructor deletes its contents. The contents in this case are pointers so it deletes the pointers. It does not delete the things the pointers point to, so neither the structs nor their members get deleted.
If you want to delete the things the pointers point to use qDeleteAll.
This will delete the structs but will not delete the spinboxes.
To delete spinboxes too give SellInfo a proper destructor that will do that. -
Hi
When you insert pointers in Qvector, all it ever will delete is the pointer as it would be unsafe for it to delete the object pointed too as it is outside the vector.
If you declare it as not pointer
QVector<SellInfo>
Then the SellInfo struct lives in the vector and will be deleted when QVec is.
Deleting a SellInfo would never delete "box" as it will not call box destructor.
(its a pointer)A good rule is that you bring into the world with new, should be removed with delete.
But in Qt, many of the Wigets takes ownership of childs and you do not need to call delete on them.