Reputation: 4858
I have derived from the QGraphicsItem
class and drawing some custom shapes. These multiple shapes are then added to a QGraphicsScene
.
class StairCurve : public QObject, public CurveShape, public QGraphicsItem
class BezierCurve: public QObject, public CurveShape, public QGraphicsItem
CurveShape
is a class which has some common properties related to these curves like ID etc. It doesn't derive from anything else. It has some pure virtual
functions though.
For deleting all the beziers from scene, I am doing something like this :
QList<QGraphicsItem*> all = selectedItems();
for (int i = 0; i < all.size(); i++)
{
QGraphicsItem * item = all[i];
if( NULL != item && (BezierCurve::Type == item->type()) )
{
removeItem(item);
delete item;
item = NULL;
}
}
QGraphicsItem * item = all[i];
will return a pointer to the base class part of my object. So calling remove on it looks good to me. This object will not be a part of scene any more.
My concern is that calling delete
on item
will call the destructor on the base class, right ? I don't think it will remove the complete object from memory. Will doing something like this make more sense :
delete dynamic_cast<BezierCurve*>(item);
More formally,
Class A : public B, public C
Class B : public D
A *a;
To delete the object a
, the correct way is using delete
operator on a
, rather than on any of the base class objects in the hierarchy, correct ?
And does the use of virtual destructors
relate to all this in any ways?
Upvotes: 3
Views: 409
Reputation: 21220
QGraphicsItem
has a virtual destructor. Thus if you call delete item;
, where item
is a pointer of type QGraphicsItem
, the destructors of all derived classes will be called first. To test this, just implement the destructor of the StairCurve
class and see whether it is called.
Upvotes: 2