PDA

View Full Version : A way to detect whether drag is going on



eelik
25th December 2008, 19:37
Is there a way to find out whether a drag operation is going on on top of a widget? It's easy if it's started on that same widget, but if the drag comes from another widget it's not so easy. The drag will be stopped when it's dropped, but I didn't find an easy way to detect whether it has been canceled. It's possible to catch the ESC key event but I think it's not guaranteed to be the only way to cancel drag.

I have a paint event which does different thing while dragging. The code detecting drag is quite complicated already and it doesn't work reliably.

jpn
25th December 2008, 19:50
How about

QWidget::dragEnterEvent()
QWidget::dragMoveEvent()
QWidget::dragLeaveEvent()

?

eelik
26th December 2008, 15:26
I don't know if I wrote unclearly or if you read my post carelessly, but those are not enough. QTreeWidget gets some events which trigger painting (type 43 IIRC, but I can't of course be sure this is the only one), and I should detect whether dragging is going on inside the paint event handler. I have tried to do it with simple boolean flags which are set and unset in some places but they are not completely reliable.

I think It would work like this:
- dragEnterEvent -> set dragging flag on
- dropEvent -> set dragging flag off
- dropLeaveEvent -> set draggin flag off

BUT because the drag can be also cancelled, this is not reliable. I would need another event:
- dragCancelEvent -> set dragging flag off

Another possibility would be to ask whether drag is going on:
QApplication::isDragging()
QWidget::isDragging()

but there are no such functions.

jpn
26th December 2008, 16:10
So do you want to still show some kind of drop indicator even if the drag is currently outside the whole view? QAbstractItemView resets the "hover index" upon:

QEvent::HoverLeave
QEvent::DragLeave

eelik
26th December 2008, 21:19
No, I wanted to show an indicator in a widget while the drag is going on on top of that widget. The problem was that I couldn't know in the paintEvent() whether the drag is going on or not because the drag could have been canceled after the state flag had been set and the paintEvent was triggered by some unexpected events.

Anyways, I found a way to do this. I though I had tested dragLeaveEvent but apparently I hadn't. That event is received also when the drag is canceled. It's not clearly documented so I don't know if I can be 100% sure but this seems to be the correct way to do it. Now it's enough to clear the dragging flag in dragLeave and drop events.