Results 1 to 6 of 6

Thread: Deleting a scene from QGraphicsItem mouseEvent

  1. #1
    Join Date
    Jan 2007
    Posts
    81
    Thanks
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Deleting a scene from QGraphicsItem mouseEvent

    Hello all,

    I have implemented QGraphicsItem, and when the item receives a mouseEvent I call a method in my implemented QGraphicsView which deletes the scene. The problem is that the event still tries to exit and crashes. I have tried to pass the scene to the item and remove the item from the scene one line before (and after) calling the method in QGraphicsView. Both of these approaches do not work.

    Items gets mouseEvent->calls QGraphicsView (forward declaration used; each has an instance of the other)->QGraphicsView deletes scene->crashes from inside QGraphicsItem.

    When my program was written with only widgets, I emitted a signal and had no troubles with deleting all the widgets, etc...

    Any thoughts anyone?

    Thanks!

    Jonathan

  2. #2
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Deleting a scene from QGraphicsItem mouseEvent

    Maybe deleteLater() on the scene would work better ?

    Check this out :
    http://doc.trolltech.com/4.2/qobject.html#deleteLater

  3. The following user says thank you to guilugi for this useful post:

    JonathanForQT4 (10th April 2007)

  4. #3
    Join Date
    Jan 2007
    Posts
    81
    Thanks
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Talking Re: Deleting a scene from QGraphicsItem mouseEvent

    I tried to use deleteLater earlier today, but to no avail....I'm guessing I used it in the wrong place and will try again.

    Thanks for confirming my thoughts.

  5. #4
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Deleting a scene from QGraphicsItem mouseEvent

    Quote Originally Posted by JonathanForQT4 View Post
    Hello all,

    I have implemented QGraphicsItem, and when the item receives a mouseEvent I call a method in my implemented QGraphicsView which deletes the scene. The problem is that the event still tries to exit and crashes. I have tried to pass the scene to the item and remove the item from the scene one line before (and after) calling the method in QGraphicsView. Both of these approaches do not work.

    Items gets mouseEvent->calls QGraphicsView (forward declaration used; each has an instance of the other)->QGraphicsView deletes scene->crashes from inside QGraphicsItem.

    When my program was written with only widgets, I emitted a signal and had no troubles with deleting all the widgets, etc...

    Any thoughts anyone?

    Thanks!

    Jonathan
    Probably you weren't setting the scene of the corresponding view to 0 before deleting.
    Try this, it works

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class View : public QGraphicsView
    4. {
    5. public:
    6. View(QGraphicsScene *sc) : QGraphicsView(sc)
    7. {}
    8.  
    9. void deleteScene()
    10. {
    11. QGraphicsScene *sc = scene();
    12. setScene(0);
    13. sc->deleteLater();
    14. //ONE BUG: The view is not updated when scene is set to 0
    15. }
    16. };
    17.  
    18. class Item : public QGraphicsEllipseItem
    19. {
    20. public:
    21. Item(QRectF rect, QGraphicsScene *scene,View *v) : QGraphicsEllipseItem(rect,0,scene)
    22. {
    23. view = v;
    24. }
    25.  
    26. void mousePressEvent(QGraphicsSceneMouseEvent * event)
    27. {
    28. scene()->removeItem(this);
    29. view->deleteScene();
    30. }
    31. View * view;
    32. };
    33.  
    34.  
    35.  
    36. int main(int argc, char *argv[])
    37. {
    38. QApplication app(argc,argv);
    39. QGraphicsScene *scene = new QGraphicsScene(0,0,800,600);
    40. View *v = new View(scene);
    41. Item *it = new Item(QRectF(10,10,40,30),scene,v);
    42. v->show();
    43. return app.exec();
    44. }
    To copy to clipboard, switch view to plain text mode 


    PS: I guess I found a bug in the GV framework. The view is not updated when its scene is set to 0. That can easily be seen in the above example.
    Please correct me if I am wrong or missing something.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  6. #5
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Deleting a scene from QGraphicsItem mouseEvent

    your_scene->deleteLater() should be available.

  7. #6
    Join Date
    Jan 2007
    Posts
    81
    Thanks
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Deleting a scene from QGraphicsItem mouseEvent

    ok, tried it again in another place and it's working wahoo!

    Thanks again

Similar Threads

  1. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  2. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  3. How to use QGraphicsView and Scene right ...
    By Mike in forum Qt Programming
    Replies: 6
    Last Post: 22nd January 2007, 08:51

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.