PDA

View Full Version : Deleting a scene from QGraphicsItem mouseEvent



JonathanForQT4
10th April 2007, 10:21
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

guilugi
10th April 2007, 10:32
Maybe deleteLater() on the scene would work better ?

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

JonathanForQT4
10th April 2007, 11:11
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.

Gopala Krishna
10th April 2007, 11:13
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


#include <QtGui>

class View : public QGraphicsView
{
public:
View(QGraphicsScene *sc) : QGraphicsView(sc)
{}

void deleteScene()
{
QGraphicsScene *sc = scene();
setScene(0);
sc->deleteLater();
//ONE BUG: The view is not updated when scene is set to 0
}
};

class Item : public QGraphicsEllipseItem
{
public:
Item(QRectF rect, QGraphicsScene *scene,View *v) : QGraphicsEllipseItem(rect,0,scene)
{
view = v;
}

void mousePressEvent(QGraphicsSceneMouseEvent * event)
{
scene()->removeItem(this);
view->deleteScene();
}
View * view;
};



int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QGraphicsScene *scene = new QGraphicsScene(0,0,800,600);
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.

guilugi
10th April 2007, 11:17
your_scene->deleteLater() should be available.

JonathanForQT4
10th April 2007, 11:27
ok, tried it again in another place and it's working :) wahoo!

Thanks again :)