View Full Version : Model View Drag Drop to non model view mouse coord offset problem.

17th April 2012, 17:52
I have a QTreeView with QStandardItems. I have a sub-classed QWigget that will accept drops from the QTreeView. It works great except for one annoying bug.
When the item is dragged over the drop widget, then released, it is not placed where it is dropped, only nearby. This is because I do not know the mouse point within the delegate that is being dragged. If I knew it, I could have it drop right where the user intended it. Instead they have to drop it and reposition it.

How can I get the mouse offset within the delegate, so that it is drag-and-drop-once?

18th April 2012, 15:27
You could implement QWidget::dropEvent() in the widget accepting the drop and tell the delegate drop coordinates there.

On another hand, why the delegate needs to know the position?
It should be the accepting widget that is interested in what is dropped and where.

18th April 2012, 16:12
Yes, absolutely true. I have implemented dropEvent on the widget accepting the drop.

And yes, the delegate does not need to know, but the accepting widget does. However, I'm still at a loss of how to get the mouse location offset within the delegate.

However I cannot figure out for the life of me, how to get the mouse location in the delgate. Currently, the drop happens, but the mouse coords are the mouse coords. Using these, the dropped item is off by however much the x,y offset is of the mouse in the delegate. That is to say given a 50x100 delegate, if I click and drag in the middle, the drop location is off by 25,50. I need to get that offset and subtract that from the mouse coords to get a "perfect drop"

18th April 2012, 18:16
Hmmmm I don't quite follow.

The drop position should be where the mouse was released.
If the coordinates that are in the drop event are wrong, try using QCursor::pos().

It's late and I'm tired today, but if you get a compilable example of your issue I (or maybe someone else) can take a look at it.

18th April 2012, 19:11
Here, let me make a picture

Now the "." in the delegate is the mouse location. When I drop this delegate on the widget, I get the location of the "." in the widget coordinates. The problem is, if I put an item at that location it is off by

So I need to subtract (10,2 - in characters) from the "." position(10,2) to get the proper place in the widget to position the item. This is the offset I am looking for.

If the mouse were at 0,0 in t he delegate, I would be all set. But it never is.