拖放一个文件进入窗口时将触发拖放事件
每个 QWidget 对象都能够处理拖放事件
拖放事件的处理函数为
-- [virtual protected] void QWidget::dragEnterEvent(QDragEnterEvent *event)
-- [virtual protected] void QWidget::dropEvent(QDropEvent *event)
[virtual protected] void QWidget::dragEnterEvent(QDragEnterEvent *event)
/*
This event handler is called when a drag is in progress and the mouse enters this widget. The event is passed in the event parameter.
If the event is ignored, the widget won't receive any drag move events.
See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.
See also QDrag and QDragEnterEvent.
*/
[virtual protected] void QWidget::dragLeaveEvent(QDragLeaveEvent *event)
/*
This event handler is called when a drag is in progress and the mouse leaves this widget. The event is passed in the event parameter.
See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.
See also QDrag and QDragLeaveEvent.
*/
[virtual protected] void QWidget::dragMoveEvent(QDragMoveEvent *event)
/*
This event handler is called if a drag is in progress, and when any of the following conditions occur: the cursor enters this widget, the cursor moves within this widget, or a modifier key is pressed on the keyboard while this widget has the focus. The event is passed in the event parameter.
See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.
See also QDrag and QDragMoveEvent.
*/
[virtual protected] void QWidget::dropEvent(QDropEvent *event)
/*
This event handler is called when the drag is dropped on this widget. The event is passed in the event parameter.
See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.
See also QDrag and QDropEvent.
*/
拖放事件中的QMimeData
-- 是Qt中的多媒体数据类
-- 拖放事件通过 QMimeData 对象传递数据
-- QMimeData 支持多种不同类型的多媒体数据
自定义拖放事件的步骤
1、对接收拖放事件的对象调用 setAcceptDrops 成员函数
2、重写 dragEnterEvent 函数并判断 MIME 类型
--- 期望数据 : e -> acceptProposedAction();
--- 其他数据 : e -> ignore();
3、重写 dropEvent 函数并判断 MIME 类型
--- 期望数据 : 从事件对象中获取 MIME 数据并处理
--- 其他数据 : e -> ignore();