PDA

View Full Version : Getting DragEvent target info



MrGarbage
21st September 2007, 21:53
Hi,

I am implementing a Drag and Drop of some LED objects.
These are embedded in some layouts which are part of a config class where
I am implementing the dragEnterEvent(), etc.

Im my LED objects' mousePressEvent() I setup QMimeData such that in my
config class dropEvent() I successfully decode the mime data that I passed in.

So I have all of the info about the source widget...

The problem is that I need some information about the target widget in
order to determine if I will actually allow the drop and what to do
with the source mime data.

There doesn't seem to be a way to find the target widget.
I have event->pos but I don't seem to be able to get this to
map to the widget where I am dropping.

This seems like it should do it, but the result is not the target object.
DraggableLED *child = static_cast<DraggableLED*>(childAt(event->pos()));

Many thanks

Mark

jpn
22nd September 2007, 08:57
One option would be to handle drag and drop events by installing an event filter on children.



bool Parent::eventFilter(QObject* receiver, QEvent* event)
{
DraggableLED* led = dynamic_cast<DraggableLED*>(receiver);
if (!led)
return false; // not a DraggableLED

switch (event->type())
{
case QEvent::DragEnter:
// do something with led
break;
...
}
return false;
}


The difference with reimplementing such event handler of the parent is that by the time event reaches parent it has already been ignored by the child:


void Parent::dragEnterEvent(..)
{
...
}

MrGarbage
22nd September 2007, 09:27
Unfortunately the child doesn't have enough info to complete the task. Only the parent
has that. I don't want some kludge like a pointer back to the parent. Seems like there
should be a way to do it.

Can't I somehow get access to the target widget from the coordinates of the
drop point event->pos()?

jpn
22nd September 2007, 09:34
Unfortunately the child doesn't have enough info to complete the task. Only the parent has that.
That's exactly why I made the "Parent" being an event filter.

MrGarbage
22nd September 2007, 10:23
I found a solution for this.

QWidget *child = childAt(event->pos());
DraggableLED *target = static_cast<DraggableLED*>(child->parentWidget());

Thanks for your input!