Jump to content

Drag and Drop within a GridView: Difference between revisions

From Qt Wiki
Niels (talk | contribs)
No edit summary
Niels (talk | contribs)
No edit summary
Line 12: Line 12:


  '''IconItem.qml'''<br /><code>import QtQuick 1.0<br />Component {<br /> Item {<br /> id: main<br /> width: grid.cellWidth; height: grid.cellHeight<br /> Image {<br /> id: item; parent: loc<br /> x: main.x + 5; y: main.y + 5<br /> width: main.width - 10; height: main.height - 10;<br /> fillMode: Image.PreserveAspectFit; smooth: true<br /> source: icon<br /> Rectangle {<br /> anchors.fill: parent;<br /> border.color: &quot;#326487&quot;; border.width: 6<br /> color: &quot;transparent&quot;; radius: 5<br /> visible: item.state  &quot;active&quot;
  '''IconItem.qml'''<br /><code>import QtQuick 1.0<br />Component {<br /> Item {<br /> id: main<br /> width: grid.cellWidth; height: grid.cellHeight<br /> Image {<br /> id: item; parent: loc<br /> x: main.x + 5; y: main.y + 5<br /> width: main.width - 10; height: main.height - 10;<br /> fillMode: Image.PreserveAspectFit; smooth: true<br /> source: icon<br /> Rectangle {<br /> anchors.fill: parent;<br /> border.color: &quot;#326487&quot;; border.width: 6<br /> color: &quot;transparent&quot;; radius: 5<br /> visible: item.state  &quot;active&quot;
             &amp;#125;
             &#125;
             Behavior on x &amp;#123; enabled: item.state != &quot;active&quot;; NumberAnimation &amp;#123; duration: 400; easing.type: Easing.OutBack &amp;#125; &amp;#125;
             Behavior on x &#123; enabled: item.state != &quot;active&quot;; NumberAnimation &#123; duration: 400; easing.type: Easing.OutBack &#125; &#125;
             Behavior on y &amp;#123; enabled: item.state != &quot;active&quot;; NumberAnimation &amp;#123; duration: 400; easing.type: Easing.OutBack &amp;#125; &amp;#125;
             Behavior on y &#123; enabled: item.state != &quot;active&quot;; NumberAnimation &#123; duration: 400; easing.type: Easing.OutBack &#125; &#125;
             SequentialAnimation on rotation &amp;#123;
             SequentialAnimation on rotation &#123;
                 NumberAnimation &amp;#123; to:  2; duration: 60 &amp;#125;
                 NumberAnimation &#123; to:  2; duration: 60 &#125;
                 NumberAnimation &amp;#123; to: -2; duration: 120 &amp;#125;
                 NumberAnimation &#123; to: -2; duration: 120 &#125;
                 NumberAnimation &amp;#123; to:  0; duration: 60 &amp;#125;
                 NumberAnimation &#123; to:  0; duration: 60 &#125;
                 running: loc.currentId != -1 &amp;amp;&amp;amp; item.state != &quot;active&quot;
                 running: loc.currentId != -1 &amp;amp;&amp;amp; item.state != &quot;active&quot;
                 loops: Animation.Infinite; alwaysRunToEnd: true
                 loops: Animation.Infinite; alwaysRunToEnd: true
             &amp;#125;
             &#125;
             states: State &amp;#123;
             states: State &#123;
                 name: &quot;active&quot;; when: loc.currentId  gridId<br /> PropertyChanges { target: item; x: loc.mouseX - width/2; y: loc.mouseY - height/2; scale: 0.5; z: 10 }<br /> }<br /> transitions: Transition { NumberAnimation { property: &quot;scale&quot;; duration: 200} }<br /> }<br /> }<br />}</code>
                 name: &quot;active&quot;; when: loc.currentId  gridId<br /> PropertyChanges { target: item; x: loc.mouseX - width/2; y: loc.mouseY - height/2; scale: 0.5; z: 10 }<br /> }<br /> transitions: Transition { NumberAnimation { property: &quot;scale&quot;; duration: 200} }<br /> }<br /> }<br />}</code>



Revision as of 12:47, 24 February 2015





English | 日本語 | Български

This wiki entry is created in response to a few forum threads (links at bottom) asking for this method. Feel free to contribute to this article with explanations or code.

Drag and Drop within a GridView

This example will demonstrate the iOS style ("video here":http://www.youtube.com/watch?v=qnXoGnUU6uI ) of dragging and dropping ListModel elements within a GridView.

There are three files in this example. One for the model (WidgetModel.qml), one for the delegate (IconItem.qml) and one for the GridView (Main.qml).

WidgetModel.qml
import QtQuick 1.0
ListModel {
ListElement { icon: "Images/widget1.png"; gridId: 0}
ListElement { icon: "Images/widget2.png"; gridId: 1}
ListElement { icon: "Images/widget3.png"; gridId: 2}
ListElement { icon: "Images/widget4.png"; gridId: 3}
ListElement { icon: "Images/widget5.png"; gridId: 4}
ListElement { icon: "Images/widget6.png"; gridId: 5}
ListElement { icon: "Images/widget7.png"; gridId: 6}
ListElement { icon: "Images/widget8.png"; gridId: 7}
ListElement { icon: "Images/widget9.png"; gridId: 8}
}

IconItem.qml

import QtQuick 1.0<br />Component {<br /> Item {<br /> id: main<br /> width: grid.cellWidth; height: grid.cellHeight<br /> Image {<br /> id: item; parent: loc<br /> x: main.x + 5; y: main.y + 5<br /> width: main.width - 10; height: main.height - 10;<br /> fillMode: Image.PreserveAspectFit; smooth: true<br /> source: icon<br /> Rectangle {<br /> anchors.fill: parent;<br /> border.color: &quot;#326487&quot;; border.width: 6<br /> color: &quot;transparent&quot;; radius: 5<br /> visible: item.state  &quot;active&quot;
            &#125;
            Behavior on x &#123; enabled: item.state != &quot;active&quot;; NumberAnimation &#123; duration: 400; easing.type: Easing.OutBack &#125; &#125;
            Behavior on y &#123; enabled: item.state != &quot;active&quot;; NumberAnimation &#123; duration: 400; easing.type: Easing.OutBack &#125; &#125;
            SequentialAnimation on rotation &#123;
                NumberAnimation &#123; to:  2; duration: 60 &#125;
                NumberAnimation &#123; to: -2; duration: 120 &#125;
                NumberAnimation &#123; to:  0; duration: 60 &#125;
                running: loc.currentId != -1 &amp;amp;&amp;amp; item.state != &quot;active&quot;
                loops: Animation.Infinite; alwaysRunToEnd: true
            &#125;
            states: State &#123;
                name: &quot;active&quot;; when: loc.currentId  gridId<br /> PropertyChanges { target: item; x: loc.mouseX - width/2; y: loc.mouseY - height/2; scale: 0.5; z: 10 }<br /> }<br /> transitions: Transition { NumberAnimation { property: &quot;scale&quot;; duration: 200} }<br /> }<br /> }<br />}
Main.qml
import QtQuick 1.0
Rectangle {
width: 640; height: 480
color: "#222222"
GridView {
id: grid
interactive: false
anchors {
topMargin: 60; bottomMargin: 60
leftMargin: 140; rightMargin: 140
fill: parent
}
cellWidth: 120; cellHeight: 120;
model: WidgetModel { id: icons }
delegate: IconItem { }
MouseArea {
property int currentId: –1 // Original position in model
property int newIndex // Current Position in model
property int index: grid.indexAt(mouseX, mouseY) // Item underneath cursor
id: loc
anchors.fill: parent
onPressAndHold: currentId = icons.get(newIndex = index).gridId
onReleased: currentId = –1
onMousePositionChanged:
if (loc.currentId = -1 && index= –1 && index != newIndex)
icons.move(newIndex, newIndex = index, 1)
}
}
}

Video
[YouTubeID:4tt5hQYS4Fo]

Relevant forum threads:
"iOS Style Rearrange of Icons":http://developer.qt.nokia.com/forums/viewthread/2327/