<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- draganddrop.qdoc --> <title>Qt Quick Examples - Drag and Drop | Qt Quick 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtquick-index.html">Qt Quick</a></td><td >Qt Quick Examples - Drag and Drop</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#running-the-example">Running the Example</a></li> <li class="level1"><a href="#tiles">Tiles</a></li> <li class="level1"><a href="#gridview-example">GridView Example</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Qt Quick Examples - Drag and Drop</h1> <span class="subtitle"></span> <!-- $$$draganddrop-brief --> <p>This is a collection of QML drag and drop examples.</p> <!-- @@@draganddrop --> <!-- $$$draganddrop-description --> <div class="descr"> <a name="details"></a> <p class="centerAlign"><img src="images/qml-draganddrop-example.png" alt="" /></p><p><i>Drag and Drop</i> is a collection of small QML examples relating to the drag and drop functionality. For more information, visit the Drag and Drop page.</p> <a name="running-the-example"></a> <h2 id="running-the-example">Running the Example</h2> <p>To run the example from Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</p> <a name="tiles"></a> <h2 id="tiles">Tiles</h2> <p><i>Tiles</i> adds drag and drop to simple rectangles, which you can drag into a specific grid.</p> <p>It has a DragTile component which uses a <a href="qml-qtquick-mousearea.html">MouseArea</a> to move an item when dragged:</p> <pre class="qml"> Item { id: root property string colorKey width: 64; height: 64 MouseArea { id: mouseArea width: 64; height: 64 anchors.centerIn: parent drag.target: tile onReleased: parent = tile.Drag.target !== null ? tile.Drag.target : root Rectangle { id: tile width: 64; height: 64 anchors.verticalCenter: parent.verticalCenter anchors.horizontalCenter: parent.horizontalCenter color: colorKey Drag.keys: [ colorKey ] Drag.active: mouseArea.drag.active Drag.hotSpot.x: 32 Drag.hotSpot.y: 32 states: State { when: mouseArea.drag.active ParentChange { target: tile; parent: root } AnchorChanges { target: tile; anchors.verticalCenter: undefined; anchors.horizontalCenter: undefined } } } } } </pre> <p>And a DropTile component on which the dragged tiles can be dropped:</p> <pre class="qml"> DropArea { id: dragTarget property string colorKey property alias dropProxy: dragTarget width: 64; height: 64 keys: [ colorKey ] Rectangle { id: dropRectangle anchors.fill: parent color: colorKey states: [ State { when: dragTarget.containsDrag PropertyChanges { target: dropRectangle color: "grey" } } ] } } </pre> <p>The keys property of the <a href="qml-qtquick-droparea.html">DropArea</a> will only allow an item to be dropped on it if it has a matching key in its Drag.keys property.</p> <a name="gridview-example"></a> <h2 id="gridview-example">GridView Example</h2> <p>The <i><a href="qml-qtquick-gridview.html">GridView</a> Example</i> adds drag and drop to a <a href="qml-qtquick-gridview.html">GridView</a>, allowing you to visually reorder the delegates without changing the underlying ListModel. It uses a DelegateModel to move a delegate item to the position of another item it is dragged over.</p> <pre class="qml"> model: DelegateModel { delegate: DropArea { id: delegateRoot width: 80; height: 80 onEntered: visualModel.items.move(drag.source.visualIndex, icon.visualIndex) property int visualIndex: DelegateModel.itemsIndex Binding { target: icon; property: "visualIndex"; value: visualIndex } Rectangle { id: icon property int visualIndex: 0 width: 72; height: 72 anchors { horizontalCenter: parent.horizontalCenter; verticalCenter: parent.verticalCenter } radius: 3 color: model.color Text { anchors.centerIn: parent color: "white" text: parent.visualIndex } DragHandler { id: dragHandler } Drag.active: dragHandler.active Drag.source: icon Drag.hotSpot.x: 36 Drag.hotSpot.y: 36 states: [ State { when: icon.Drag.active ParentChange { target: icon parent: root } AnchorChanges { target: icon anchors.horizontalCenter: undefined anchors.verticalCenter: undefined } } ] } } </pre> <p>Files:</p> <ul> <li><a href="qtquick-draganddrop-draganddrop-pro.html">draganddrop/draganddrop.pro</a></li> <li><a href="qtquick-draganddrop-draganddrop-qml.html">draganddrop/draganddrop.qml</a></li> <li><a href="qtquick-draganddrop-draganddrop-qmlproject.html">draganddrop/draganddrop.qmlproject</a></li> <li><a href="qtquick-draganddrop-draganddrop-qrc.html">draganddrop/draganddrop.qrc</a></li> <li><a href="qtquick-draganddrop-main-cpp.html">draganddrop/main.cpp</a></li> <li><a href="qtquick-draganddrop-tiles-dragtile-qml.html">draganddrop/tiles/DragTile.qml</a></li> <li><a href="qtquick-draganddrop-tiles-droptile-qml.html">draganddrop/tiles/DropTile.qml</a></li> <li><a href="qtquick-draganddrop-tiles-tiles-qml.html">draganddrop/tiles/tiles.qml</a></li> <li><a href="qtquick-draganddrop-views-gridview-qml.html">draganddrop/views/gridview.qml</a></li> </ul> </div> <!-- @@@draganddrop --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> 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. </p> </div> </body> </html>