PDA

View Full Version : destruction of QGraphicsItem



killkolor
4th December 2006, 19:57
This is about destroying QGraphicsItem instances. I partly refer to this thread here:
http://lists.trolltech.com/qt4-preview-feedback/2006-09/thread00056-0.html

What I've tried is to implement a one shoot delete for QGraphicsItem as in the thread above. I'll give my code here:




// this is in the actual object that is deleted by pressing the del key.
// edge is a subclass of QGraphicsItem. first the edge is retrieved (once
// as an edge and once as item), then the lists are updated and finally
// the edges and the current node are remembered for deleting.

void
DrawNode::keyPressEvent(QKeyEvent* e)
{
if (e->key() == Qt::Key_Delete) {
for (int i = 0; i < edgeList.count(); i++)
{
Edge *edge = edgeList[i];
QGraphicsItem *item = edgeList[i];

if (edge && edge->sourceNode() != this)
{
if (edge->sourceNode()->edges().contains(edge))
edge->sourceNode()->edges().removeAll(edge);
}
else if (edge && edge->destNode() != this)
{
if (edge->destNode()->edges().contains(edge))
int removed = edge->destNode()->edges().removeAll(edge);
}
graph->deleteItemLater(item);
}
graph->deleteItemLater(this);
}
}

=====================

// this is a signal/slot one shoot timer for the deletion of the scheduled
// objects. it is implemented in GraphWidget, which is a subclass of QGraphicsView

void
GraphWidget::deleteItemLater(QGraphicsItem* item)
{
m_itemsToDelete.append(item);
QTimer::singleShot(1000, this, SLOT(deleteItems()));
}

// private slot
void
GraphWidget::deleteItems()
{
foreach (QGraphicsItem* item, m_itemsToDelete) {
scene()->removeItem(item);
delete item;
item = NULL;
}
m_itemsToDelete.clear();
}



when deleting an object everything is fine, but trying to move the adjacent node the program crashes here:



void Edge::adjust()
{
if (!source || !dest /*|| !(source->getGraph()) || !(dest->getGraph())*/)
return;

QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
....
}


As you can see this code is executed inside the edge class. The this pointer evaluates to a valid address, but the QGraphicsItem part (Edge is subclassed from QGraphicsItem) has the sound name 0xfeeefeee. the same goes for the two node members which are also subclasses of QGraphicsItem.
Somehow it seems that the edge is not really deleted (i can't say for the node so far), but only it's QGraphicsItem pointer set to a strange value.
Does anybody know how I can delete delete my Edges and Nodes?

thanks!

killkolor
4th December 2006, 21:55
problem solved. i the first part of the code, when i was deleting the edges from the edge lists, i copied the list and deleted from the copy... stupid mistake

naren82
5th December 2009, 10:31
First of all look up into ur code if you are any how capturing Delete key. Check if you have used :

setShortcut(tr("Delete"));

any where in your code.

As this will capture Delete key on application level prevent the KeyPressEvent to capture Qt::Key_Delete. I have also faced the similar problem. The moment i commented this piece of line KeyPressEvent captured the Qt::Key_Delete. Thiswas the only possible way we were not able to capture key Qt::Key_Delete with keypressevent.