Graphics item position after drag
Hi,
I've been trying to implement drag&drop functionality in my custom QGraphicsItem object. I've implemented three derived methods: mousePressEvent, mouseReleaseEvent and mouseMoveEvent.
If I simply click an item in scene, press and release events are called. If I try to drag an item, press and move events are called, but release event is not. Why is that?
The problem is that I'd need to get the scene coordinates where I drop the item after dragging. One solution might be to get QCursor::pos() and map it to scene coordinate system, but I guess there is an easier way to do this?
Re: Graphics item position after drag
What are you trying to do with drag and drop ?
If you simply want to move the item in the scene, you dont need to implement drag drop yourself... you just need to set the flags,,QGraphicsItem::setFlags , QGraphicsItem::ItemIsMovable :)
But if your requirement is implementing drag drop yourself, have a look at -
QGraphicsItem::dragEnterEvent
QGraphicsItem::dragLeaveEvent
QGraphicsItem::dragMoveEvent
QGraphicsItem::dropEvent
Hope this helps
1 Attachment(s)
Re: Graphics item position after drag
Thanks for the info, it seems that this was the easy solution I was looking for :)
I dumbed the mousePressEvent etc. methods and simply set appropriate flag in my graphic item's ctor and... well, it kinda worked. I tested with a simple rectangle, it can be dragged but it leaves a trail in the scene (see attached capture). The trail disappears once the screen is refreshed, e.g. focusing other window and re-focusing my application. Dragging a pixmap didn't have this kind of effect... I'll keep investigating this.
Re: Graphics item position after drag
...and a second after I clicked the Submit button, I realized where the problem is...
The black rectangle in previous post is drawn as a border for pixmap. However, my boundingRect function returned the rect for pixmap only and didn't calculate the border. That was also the reason why it worked well with pixmap... Once I fixed boundingRect return value it started to work slightly better :)
There are still some problems when there are several items shown, but I'll keep on digging.