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!
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!