QT拖放事件剖析(一)

本文介绍了QT中的拖放事件处理,包括dragEnterEvent和dropEvent两个关键函数。QMimeData在拖放事件中扮演重要角色,用于传递多种类型的数据。实现自定义拖放事件需设置setAcceptDrops,重写dragEnterEvent和dropEvent,根据MIME类型进行数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

拖放一个文件进入窗口时将触发拖放事件

每个 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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值