Qt-UI
Drag and Drop
← Specializing QWidget | ● | Drag and Dropping URLs →
Qt’s Drag and Drop concept is also realized with events.
There are events for a dragged item
- entering a widgets area
- moving in a widgets area
- leaving a widgets area
- dropped within a widgets area
There are according virtual methods that are dispatched when the above events are delivered:
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dragLeaveEvent(QDragLeaveEvent *event);
public:
void dropEvent(QDropEvent *event);
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dragLeaveEvent(QDragLeaveEvent *event);
public:
void dropEvent(QDropEvent *event);
For that to work, we need to enable drag and drop support in the widget’s constructor:
setAcceptDrops(true);
In case a widget wants to accept a particular item of a specific type being dragged into its area we need to notify this in the respective event handler:
void ViewerWindow::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
{
event->acceptProposedAction();
}
The same applies for the move and drop events.