PDA

View Full Version : QGraphicsScene drag event



rbp
11th February 2009, 04:29
hello,

I'm trying to catch the drag event in QGraphicsScene so I can draw when dragging the mouse. There is such a function in the API so it should be possible:
http://doc.trolltech.com/4.4/qgraphicsscene.html#dragEnterEvent

But I can't seem to trigger this event. I can catch mouse move, click, and release, but not drag enter or drag move.

There seems to be a lot of confusion on this topic because there are many threads about it with conflicting information.
In this thread (http://www.qtcentre.org/forum/f-qt-programming-2/t-drar-and-drop-on-qgraphicsview-17090.html) wysota says the event can be overloaded but aamer4yu says it can't (and they are both experts).
In this thread (http://www.qtcentre.org/forum/f-qt-programming-2/t-qgraphicsscene-doesnt-enter-drag-event-14200.html) some suggest that qgraphicsitem needs to be subclassed instead.
In this thread (http://www.qtcentre.org/forum/f-qt-programming-2/t-qgraphicsscene-doesnt-accept-drops-8022.html) subclassing the event seems to have worked and it was suggested setting the drag mode property in qgraphicsview.

So, how would you suggest I catch mouse drag events so I can draw in the scene?

thanks,
Richard

aamer4yu
11th February 2009, 05:51
Referring to the first thread link... I guess i had expressed myself clearly and wysota had misunderstood.

Anyways, coming to your post..

I'm trying to catch the drag event in QGraphicsScene so I can draw when dragging the mouse.

What do you want to draw ?
If you want to draw the item itself, you dont have to implement drag functionality yourself. You just need to call QGraphicsItem::setFlags(QGraphicsItem::ItemIsMovab le);

If you can elaborate more on what you want to achieve, it wud be better.

rbp
11th February 2009, 06:19
By draw I mean like for a paint program. I want to annotate a background image so if I could catch the drag event then I would call addLine()
http://doc.trolltech.com/4.4/qgraphicsscene.html#addLine

aamer4yu
11th February 2009, 07:04
If thats the case, drag events are not relevant.
You will need to override mousePress, mouseMove and mouseRelease events of the scene.
You can contruct a polygon out of the move events, and add a item as u like to the scene.

Also if only drawing is your aim, you wont even need graphics scene or view. You can simply draw on a widget. See Scribble example under Widgets in Qt Demo.

Hope this helps :)

rbp
11th February 2009, 11:12
Thanks for your advice aamer4yu.
Unfortunately I found mousemove doesn't get called when the mouse button is down. That's why I'm trying the drag events.
Do you know if the scene drag events can be made to work?


Also if only drawing is your aim, you wont even need graphics scene or view. You can simply draw on a widget.
I want to keep the background image separate from the drawing so I can support erase efficiently. And the scene/view stack supports zooming.

Could I get erase and zoom working efficiently on a widget?

rbp
11th February 2009, 23:35
I figured it out!!

To support drag events for QGraphicsScene you must allow drops on the scene

setAcceptDrops(true);

create a QDrag object with mime data in the mousePressEvent()


QDrag *drag = new QDrag(this);
drag->setMimeData(new QMimeData()); // does not work without mime data
drag->exec();

and accept the drag event in dragEnterEvent()

void dragEnterEvent(QDragEnterEvent *event) {
event->acceptProposedAction();
}

Then you can catch the dragMoveEvent()

Phew - that's a lot of work. I would certainly prefer if mouseMoveEvent() still worked when the mouse button was down.

aamer4yu
12th February 2009, 04:22
What functionality are u trying to achieve with your application ?
I am still not able to understand why you need drag drop handling.:confused:

rbp
12th February 2009, 04:31
What functionality are u trying to achieve with your application ?
you already asked that question and I answered!

I don't need drag/drop handling but I need to tell when the mouse is moving and the button is down, and for some reason the mousemove event isn't being called when the button is down.
Anyway, I've got the mouse events I want now with the drag events.
Thanks for trying to help.

aamer4yu
12th February 2009, 05:22
you already asked that question and I answered!

I don't need drag/drop handling but I need to tell when the mouse is moving and the button is down, and for some reason the mousemove event isn't being called when the button is down.
Anyway, I've got the mouse events I want now with the drag events.
Thanks for trying to help.
Reply With Quote

That is fine. But I wanted to know are u trying to move graphics Item ? or are u trying to change color / monitor cursor position of hover over graphics item ?

You had mentioned you are trying paint like program. And I also mentioned graphics view isnt that necessary for that.
And without code of what you have tried, it is very difficult to say where you are going wrong.

rbp
13th February 2009, 07:02
That is fine. But I wanted to know are u trying to move graphics Item ? or are u trying to change color / monitor cursor position of hover over graphics item ?

I was after zoom with scrollbars and separate layers for the background image and foreground annotations. So that's what I have now with the scene/view stack.
Later I may have to add the ability to adjust control points of an annotation, but hopefully not.

I've implemented a similar widget in FLTK and it was a pain. Qt is fortunate to have higher level widgets like the graphic scene/view.

Richard

MarkoSan
13th May 2009, 14:03
Hi to all!

I have some widgets embedded into QGraphicsView via QGraphicsProxies. Now, how do I prevent those widgets from being dragged arround view. They must not be moved. Method setDragMode does not work. Please help!