Drag
For specifying drag and drop events for moved Items More...
Attached Properties
- active : bool
- hotSpot : QPointF
- keys : stringlist
- proposedAction : enumeration
- source : Object
- supportedActions : flags
- target : Object
Attached Methods
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, this can include 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.
Attached Property Documentation
This property holds whether a drag event sequence is currently active.
Setting this property to true will 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.
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 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 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 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 Method Documentation
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.
The optional supportedActions argument can be used to override the supportedActions property for the started sequence.