Results 1 to 3 of 3

Thread: destruction of QGraphicsItem

  1. #1

    Default destruction of QGraphicsItem

    This is about destroying QGraphicsItem instances. I partly refer to this thread here:
    http://lists.trolltech.com/qt4-previ...ad00056-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:

    Qt Code:
    1. // this is in the actual object that is deleted by pressing the del key.
    2. // edge is a subclass of QGraphicsItem. first the edge is retrieved (once
    3. // as an edge and once as item), then the lists are updated and finally
    4. // the edges and the current node are remembered for deleting.
    5.  
    6. void
    7. DrawNode::keyPressEvent(QKeyEvent* e)
    8. {
    9. if (e->key() == Qt::Key_Delete) {
    10. for (int i = 0; i < edgeList.count(); i++)
    11. {
    12. Edge *edge = edgeList[i];
    13. QGraphicsItem *item = edgeList[i];
    14.  
    15. if (edge && edge->sourceNode() != this)
    16. {
    17. if (edge->sourceNode()->edges().contains(edge))
    18. edge->sourceNode()->edges().removeAll(edge);
    19. }
    20. else if (edge && edge->destNode() != this)
    21. {
    22. if (edge->destNode()->edges().contains(edge))
    23. int removed = edge->destNode()->edges().removeAll(edge);
    24. }
    25. graph->deleteItemLater(item);
    26. }
    27. graph->deleteItemLater(this);
    28. }
    29. }
    30.  
    31. =====================
    32.  
    33. // this is a signal/slot one shoot timer for the deletion of the scheduled
    34. // objects. it is implemented in GraphWidget, which is a subclass of QGraphicsView
    35.  
    36. void
    37. GraphWidget::deleteItemLater(QGraphicsItem* item)
    38. {
    39. m_itemsToDelete.append(item);
    40. QTimer::singleShot(1000, this, SLOT(deleteItems()));
    41. }
    42.  
    43. // private slot
    44. void
    45. GraphWidget::deleteItems()
    46. {
    47. foreach (QGraphicsItem* item, m_itemsToDelete) {
    48. scene()->removeItem(item);
    49. delete item;
    50. item = NULL;
    51. }
    52. m_itemsToDelete.clear();
    53. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. void Edge::adjust()
    2. {
    3. if (!source || !dest /*|| !(source->getGraph()) || !(dest->getGraph())*/)
    4. return;
    5.  
    6. QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
    7. ....
    8. }
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2

    Default Re: destruction of QGraphicsItem

    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

  3. #3
    Join Date
    Mar 2009
    Posts
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: destruction of QGraphicsItem

    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.

Similar Threads

  1. adding QGraphicsItem to multiple scenes
    By forrestfsu in forum Qt Programming
    Replies: 7
    Last Post: 6th November 2006, 14:25
  2. QGraphicsItem problem - how to save items info ??
    By aamer4yu in forum Qt Programming
    Replies: 3
    Last Post: 17th October 2006, 12:17
  3. (QT4.2-RC1) QGraphicsScene QGraphicsView QGraphicsItem
    By antonio.r.tome in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 10:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.