QDropEvent Class

QDropEvent 类提供了一个在拖放操作完成时发送的事件。更多

Header: #include <QDropEvent>
CMake.QDropEvent find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
继承: QEvent
继承于:

QDragMoveEvent

公共函数

QDropEvent(const QPointF &pos, Qt::DropActions actions, const QMimeData *data, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, QEvent::Type type = Drop)
void acceptProposedAction()
(since 6.0) Qt::MouseButtons buttons() const
Qt::DropAction dropAction() const
const QMimeData *mimeData() const
(since 6.0) Qt::KeyboardModifiers modifiers() const
(since 6.0) QPointF position() const
Qt::DropActions possibleActions() const
Qt::DropAction proposedAction() const
void setDropAction(Qt::DropAction action)
QObject *source() const

详细说明

当一个部件accepts drop events 时,如果它接受了最近发送给它的QDragEnterEventQDragMoveEvent ,它就会收到该事件。

Drop 事件包含一个建议的操作(可从proposedAction() 获取),供部件接受或忽略。如果小部件可以处理该操作,则应调用acceptProposedAction() 函数。由于建议的操作可以是Qt::DropAction 值的组合,因此选择其中一个值作为默认操作或要求用户选择其首选操作可能会很有用。

如果建议的下拉操作不合适,可能是因为您的自定义 widget 不支持该操作,您可以通过调用setDropAction() 并使用您首选的操作,将其替换为possible drop actions 中的任何操作。如果您设置的值不在possibleActions() 返回值的位相 OR 组合中,则将使用默认的复制操作。一旦设置了替换下拉操作,请调用accept() 而不是acceptProposedAction() 来完成下拉操作。

mimeData() 函数以QMimeData 对象的形式提供投放到 widget 上的数据。除数据本身外,该对象还包含有关数据 MIME 类型的信息。

另请参阅 QMimeData,QDrag, 以及拖放

成员函数文档

QDropEvent::QDropEvent(const QPointF &pos, Qt::DropActions actions, const QMimeData *data, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, QEvent::Type type = Drop)

构建某个type 的拖放事件,该事件对应于在目标 widget 坐标系中由pos 指定的点上的拖放操作。

actions 表明可以执行哪种类型的拖放操作,拖放数据以 MIME 编码数据的形式存储在data 中。

拖放时鼠标按钮和键盘修改器的状态由buttonsmodifiers 指定。

void QDropEvent::acceptProposedAction()

将下拉操作设置为建议的操作。

另请参阅 setDropAction()、proposedAction() 和accept()。

[since 6.0] Qt::MouseButtons QDropEvent::buttons() const

返回按下的鼠标按钮。

此函数在 Qt 6.0 中引入。

Qt::DropAction QDropEvent::dropAction() const

返回目标对数据执行的操作。如果调用setDropAction() 明确选择了下拉操作,该操作可能与proposedAction() 中提供的操作不同。

另请参阅 setDropAction()。

const QMimeData *QDropEvent::mimeData() const

返回在 widget 上丢弃的数据及其相关的 MIME 类型信息。

[since 6.0] Qt::KeyboardModifiers QDropEvent::modifiers() const

返回按下的修饰符键。

此函数在 Qt 6.0 中引入。

[since 6.0] QPointF QDropEvent::position() const

返回执行下拉操作的位置。

此函数在 Qt 6.0 中引入。

Qt::DropActions QDropEvent::possibleActions() const

返回可能的下拉操作的 OR 组合。

另请参阅 dropAction()。

Qt::DropAction QDropEvent::proposedAction() const

返回建议的删除操作。

另请参阅 dropAction()。

void QDropEvent::setDropAction(Qt::DropAction action)

设置目标对数据执行的action 。使用此操作可以用possible actions 中的一个覆盖proposed action

如果您设置的拖放操作不是可能的操作之一,拖放操作将默认为复制操作。

提供替代拖放操作后,请调用accept() 而不是acceptProposedAction()。

另请参阅 dropAction()。

QObject *QDropEvent::source() const

如果拖动操作的源是此应用程序中的一个部件,则此函数返回该源;否则返回nullptr 。操作源是用于实例化拖动的QDrag 对象的第一个参数。

如果您的部件在拖动到自身时需要特殊行为,此函数将非常有用。

另请参阅 QDrag::QDrag()。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.