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
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
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.
Re: Deleting a scene from QGraphicsItem mouseEvent
Quote:
Originally Posted by
JonathanForQT4
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
Code:
#include <QtGui>
{
public:
{}
void deleteScene()
{
setScene(0);
sc->deleteLater();
//ONE BUG: The view is not updated when scene is set to 0
}
};
{
public:
{
view = v;
}
{
scene()->removeItem(this);
view->deleteScene();
}
View * view;
};
int main(int argc, char *argv[])
{
View *v = new View(scene);
Item
*it
= new Item
(QRectF(10,
10,
40,
30),scene,v
);
v->show();
return app.exec();
}
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.
Re: Deleting a scene from QGraphicsItem mouseEvent
your_scene->deleteLater() should be available.
Re: Deleting a scene from QGraphicsItem mouseEvent
ok, tried it again in another place and it's working :) wahoo!
Thanks again :)