PDA

View Full Version : Drar and Drop on QGraphicsView



rippa
18th November 2008, 18:44
Hello. Im dragging from widget to QGraphicsView. Is it correct that the only way to accept drop is to create a QGraphicsItem in QGraphicsScene connected to my QGraphicsView and accept drops with this item?
I mean is there no way to accept drag and drop directly by QGraphicsScene?

aamer4yu
18th November 2008, 19:30
I guess u are on right track..
graphics scene accepting drops by default doesnt make sense, as graphics view framework is not designed for a particular thing... there can be many user defined items, operations.

rippa
18th November 2008, 20:56
I guess u are on right track..
graphics scene accepting drops by default doesnt make sense, as graphics view framework is not designed for a particular thing... there can be many user defined items, operations.

I see.. Not too logical in my opinion.. I dont want graphics scene to accept drops by default. I wanted to overload dropEvent function.
For example im doing something like Form designer. I want to drag some widget description in graphics view and when i drop i want corresponding graphic item added to graphic scene.
But now at start i must add some basic graphics item to scene, track scene resize and resize this widget as well and every drop is processed through this widget..
But, anyway, this is a solution. Thanks.

wysota
19th November 2008, 10:02
I guess u are on right track..
graphics scene accepting drops by default doesnt make sense, as graphics view framework is not designed for a particular thing... there can be many user defined items, operations.

No, this is wrong. You can subclass QGraphicsScene and reimplement the appropriate event handler.

rippa
19th November 2008, 10:24
No, this is wrong. You can subclass QGraphicsScene and reimplement the appropriate event handler.

Unfortunately it won't help as QGraphicsScene::dropEvent redirects drop to the first graphics item that accept drop, so u should reimplement this item's dropEvent if you want to handle drops from foreign widgets.

rippa
19th November 2008, 11:44
Well, i added graphics item, reimplemented dragEnterEvent and dropEvent and drop stil doesnt happen ((
Ive attached a small example. I'd appreciate if someone tells me what i'm doing wrong.

rippa
19th November 2008, 11:53
added these 3 functions and finally it works

void dragEnterEvent ( QGraphicsSceneDragDropEvent * event )
{
cout << "drag entered in graphics item 2" << endl;
event->acceptProposedAction();
}
void dropEvent ( QGraphicsSceneDragDropEvent * event )
{
cout << "dropped in graphics item 2" << endl;
event->acceptProposedAction();
}

void dragMoveEvent ( QGraphicsSceneDragDropEvent * event )
{
cout << "drag moved in graphics item 2" << endl;
event->acceptProposedAction();
}

Still maybe there is a way to accept drops by graphics scene directly?

wysota
19th November 2008, 15:59
Unfortunately it won't help as QGraphicsScene::dropEvent redirects drop to the first graphics item that accept drop, so u should reimplement this item's dropEvent if you want to handle drops from foreign widgets.

No, because the method is virtual so you can reimplement it. If you don't call the base class implementation, no item will get the event.

rippa
19th November 2008, 17:02
Yes, did it already ) The most confusing moment was when you want to drop on "normal" widget you need to reimplement dropEvent and dragEnterEvent, but when you drop on QGraphcisView, you need to reimplement dragMoveEvent too.