QML 的 DragEvent = QQuickDropEvent
这在QML DragEvent documentation 的更新版本中有所说明,来自sources
/*!
\qmltype DragEvent
\instantiates QQuickDropEvent
\inqmlmodule QtQuick
\ingroup qtquick-input-events
\brief Provides information about a drag event
The position of the drag event can be obtained from the \l x and \l y
properties, and the \l keys property identifies the drag keys of the event
\l source.
The existence of specific drag types can be determined using the \l hasColor,
\l hasHtml, \l hasText, and \l hasUrls properties.
The list of all supplied formats can be determined using the \l formats property.
Specific drag types can be obtained using the \l colorData, \l html, \l text,
and \l urls properties.
A string version of any available mimeType can be obtained using \l getDataAsString.
*/
QQuickDropEvent 的标头在src/quick/items/qquickdroparea_p.h 中可用。我猜这又是一个私有头文件,这意味着您不应该在 C++ 中处理这些事件,而是在 Javascript 中。
QQuickDropEvent 实例包含您无法直接访问的 QDropEvent 类型的私有实例变量。
编辑:您可能希望在 Javascript 中复制 drop 的属性并将数组传递给 C++(未经测试):
DropArea {
var actionIdToString = function(id) {
if (id === Qt.CopyAction) return 'Qt.CopyAction';
if (id === Qt.MoveAction) return 'Qt.MoveAction';
if (id === Qt.LinkAction) return 'Qt.LinkAction';
if (id === Qt.IgnoreAction) return 'Qt.IgnoreAction';
return 'Unexpected action';
}
onDropped: {
var dropProperties = [
drop.accepted,
actionIdToString(drop.action),
drop.drag.source,
drop.keys,
drop.supportedActions,
drop.x,
drop.y
]
root.dropSignal(dropProperties);
}
}
有一篇关于passing more complex datastructures between C++ and QML 的博文。据我所知,在将 Javascript 数组传递到您的插槽时,您会得到一个 QVariantList。
您可能需要考虑使用 Javascript 对象 (JSON style),以便通过名称而不是 C++ 中的索引来访问 drop 的属性,但我不确定对象在从 QML 到 C++ 的步骤中存活的情况。