Hi,
I have a problem with deleting QGraphicsItems from a scene. Item (pointer) was created by "new" in widget.cpp so it should be destroyed with "delete" anywhere - in every class. I'm trying to delete item (this) in advance function (located i n paliwo class) with is responsible for animation of my obiect:
void paliwo::advance(int phase)
{
if(warunek){
QList<QGraphicsItem*>Lista;
samolot *temp_1;
Lista = scene()->items(Qt::AscendingOrder);
for(int i = 0;i<Lista.size();++i)
if(Lista.value(i)->zValue() == 4)
temp = Lista.value(i);
temp_1 = dynamic_cast <samolot*>(temp);
temp_1->pobierz(true);
Lista.clear();
warunek = false;
}
if (!phase)
return;
else
setPos(this->x(),this->y()+3);
if (pos().y() > 300){
this->~paliwo();
}
}
paliwo::~paliwo()
{
scene()->removeItem(this);
delete this;
}
void paliwo::advance(int phase)
{
if(warunek){
QList<QGraphicsItem*>Lista;
QGraphicsItem* temp;
samolot *temp_1;
Lista = scene()->items(Qt::AscendingOrder);
for(int i = 0;i<Lista.size();++i)
if(Lista.value(i)->zValue() == 4)
temp = Lista.value(i);
temp_1 = dynamic_cast <samolot*>(temp);
temp_1->pobierz(true);
Lista.clear();
Lista.~QList();
warunek = false;
}
if (!phase)
return;
else
setPos(this->x(),this->y()+3);
if (pos().y() > 300){
this->~paliwo();
}
}
paliwo::~paliwo()
{
scene()->removeItem(this);
delete this;
}
To copy to clipboard, switch view to plain text mode
- connected with widget.cpp:
timer_piechota
= new QTimer(this);
connect(timer_piechota, SIGNAL(timeout()),scena,SLOT(advance()));
connect(timer_piechota, SIGNAL(timeout()),scena,SLOT(update()));
timer_piechota->start(10);
timer_piechota = new QTimer(this);
connect(timer_piechota, SIGNAL(timeout()),scena,SLOT(advance()));
connect(timer_piechota, SIGNAL(timeout()),scena,SLOT(update()));
timer_piechota->start(10);
To copy to clipboard, switch view to plain text mode
Object in advance function has parent, so as I guess it shoud be easly removable with "delete". When I'am destroying parent program runs normaly, but after calling destructor of item which I want to destroy program crashes. I don't know what to do
I don' t want to destroy his parent but for now its only way to destroy it. Please help.
Bookmarks