Drag QML Type
For specifying drag and drop events for moved Items. More...
Import Statement: | import QtQuick 2.15 |
Attached Properties
- active : bool
- dragType : enumeration
- hotSpot : QPointF
- imageSource : QUrl
- keys : stringlist
- mimeData : stringlist
- proposedAction : enumeration
- source : Object
- supportedActions : flags
- target : Object
Attached Signals
- dragFinished(DropAction dropAction)
- dragStarted()
Attached Methods
- void cancel()
- enumeration drop()
- void start(flags supportedActions)
- void startDrag(flags supportedActions)
Detailed Description
Using the Drag attached property, any Item can be made a source of drag and drop events within a scene.
When a drag is active on an item, any change in that item's position will generate a drag event that will be sent to any DropArea that intersects with the new position of the item. Other items which implement drag and drop event handlers can also receive these events.
The following snippet shows how an item can be dragged with a MouseArea. However, dragging is not limited to mouse drags; anything that can move an item can generate drag events, including touch events, animations and bindings.
import QtQuick 2.0 Item { width: 200; height: 200 DropArea { x: 75; y: 75 width: 50; height: 50 Rectangle { anchors.fill: parent color: "green" visible: parent.containsDrag } } Rectangle { x: 10; y: 10 width: 20; height: 20 color: "red" Drag.active: dragArea.drag.active Drag.hotSpot.x: 10 Drag.hotSpot.y: 10 MouseArea { id: dragArea anchors.fill: parent drag.target: parent } } }
A drag can be terminated either by canceling it with Drag.cancel() or setting Drag.active to false, or it can be terminated with a drop event by calling Drag.drop(). If the drop event is accepted, Drag.drop() will return the drop action chosen by the recipient of the event, otherwise it will return Qt.IgnoreAction.
See also Qt Quick Examples - Drag and Drop and Qt Quick Examples - externaldraganddrop.
Attached Property Documentation
Drag.active : bool |
This property holds whether a drag event sequence is currently active.
Binding this property to the active property of MouseArea::drag will cause startDrag to be called when the user starts dragging.
Setting this property to true will also send a QDragEnter event to the scene with the item's current position. Setting it to false will send a QDragLeave event.
While a drag is active any change in an item's position will send a QDragMove event with item's new position to the scene.
Drag.dragType : enumeration |
This property indicates whether to automatically start drags, do nothing, or to use backwards compatible internal drags. The default is to use backwards compatible internal drags.
A drag can also be started manually using startDrag.
- Drag.None - do not start drags automatically
- Drag.Automatic - start drags automatically
- Drag.Internal (default) - start backwards compatible drags automatically
When using Drag.Automatic
you should also define mimeData and bind the active property to the active property of MouseArea : MouseArea::drag.active
This property was introduced in Qt 5.2.
This property holds the drag position relative to the top left of the item.
By default this is (0, 0).
Changes to hotSpot trigger a new drag move with the updated position.
This property holds the URL of the image which will be used to represent the data during the drag and drop operation. Changing this property after the drag operation has started will have no effect.
The example below uses an item's contents as a drag image:
import QtQuick 2.12 Item { width: 200; height: 200 Rectangle { anchors.centerIn: parent width: text.implicitWidth + 20; height: text.implicitHeight + 10 color: "green" radius: 5 Drag.active: dragHandler.active Drag.dragType: Drag.Automatic Drag.supportedActions: Qt.CopyAction Drag.mimeData: { "text/plain": "Copied text" } Text { id: text anchors.centerIn: parent text: "Drag me" } DragHandler { id: dragHandler onActiveChanged: if (active) { parent.grabToImage(function(result) { parent.Drag.imageSource = result.url; }) } } } }
This property was introduced in Qt 5.8.
See also Item::grabToImage().
This property holds a list of keys that can be used by a DropArea to filter drag events.
Changing the keys while a drag is active will reset the sequence of drag events by sending a drag leave event followed by a drag enter event with the new source.
This property holds a map of mimeData that is used during startDrag.
This property was introduced in Qt 5.2.
Drag.proposedAction : enumeration |
This property holds an action that is recommended by the drag source as a return value from Drag.drop().
Changes to proposedAction will trigger a move event with the updated proposal.
This property holds an object that is identified to recipients of drag events as the source of the events. By default this is the item that the Drag property is attached to.
Changing the source while a drag is active will reset the sequence of drag events by sending a drag leave event followed by a drag enter event with the new source.
This property holds return values of Drag.drop() supported by the drag source.
Changing the supportedActions while a drag is active will reset the sequence of drag events by sending a drag leave event followed by a drag enter event with the new source.
While a drag is active this property holds the last object to accept an enter event from the dragged item, if the current drag position doesn't intersect any accepting targets it is null.
When a drag is not active this property holds the object that accepted the drop event that ended the drag, if no object accepted the drop or the drag was canceled the target will then be null.
Attached Signal Documentation
This signal is emitted when a drag finishes and the drag was started with the startDrag() method or started automatically using the dragType property.
dropAction holds the action accepted by the target item.
Note: The corresponding handler is onDragFinished
.
See also drop().
This signal is emitted when a drag is started with the startDrag() method or when it is started automatically using the dragType property.
Note: The corresponding handler is onDragStarted
.
Attached Method Documentation
enumeration drop() |
Ends a drag sequence by sending a drop event to the target item.
Returns the action accepted by the target item. If the target item or a parent doesn't accept the drop event then Qt.IgnoreAction will be returned.
The returned drop action may be one of:
- Qt.CopyAction Copy the data to the target
- Qt.MoveAction Move the data from the source to the target
- Qt.LinkAction Create a link from the source to the target.
- Qt.IgnoreAction Ignore the action (do nothing with the data).
Starts sending drag events. Used for starting old-style internal drags. startDrag is the new-style, preferred method of starting drags.
The optional supportedActions argument can be used to override the supportedActions property for the started sequence.
Starts sending drag events.
The optional supportedActions argument can be used to override the supportedActions property for the started sequence.
© 2024 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.