Deleting and Disposing of QGraphicsItem Objects
I got from class QGraphicsItem
and draw some custom shapes. These numerous forms are then added to QGraphicsScene
.
class StairCurve : public QObject, public CurveShape, public QGraphicsItem
class BezierCurve: public QObject, public CurveShape, public QGraphicsItem
CurveShape
is a class that has some common properties associated with these curves, such as ID, etc. It doesn't come from anything else. It has some functions pure virtual
.
To remove all beziers from the scene, I do 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 portion of my object. So calling delete on it looks good to me. This object will no longer be part of the scene.
I'm worried that the call delete
to item
n will call the destructor in the base class, right? I don't think it will remove the entire object from memory. Would do something like this:
delete dynamic_cast<BezierCurve*>(item);
More formally
Class A : public B, public C
Class B : public D
A *a;
To delete an object a
, the correct way is to use the operator delete
on a
, and not on any base class object in the hierarchy, right?
And does this use it virtual destructors
all anyway?
source to share
QGraphicsItem
has a virtual destructor . Thus, if you call delete item;
where item
is a type pointer QGraphicsItem
, the destructors of all derived classes will be called first. To test this, just execute the class destructor StairCurve
and see if it gets called.
source to share