Drag and Drop within a GridView/ja
Jump to navigation
Jump to search
この記事はいくつかのフォーラムのスレッド(下部にリンクしました)での質問と回答を受けて作成されたものです。コードや解説でのこの記事への貢献を歓迎します。
GridView でのドラッグ&ドロップ
このサンプルは iOS スタイルのドラッグ&ドロップ( "参考ビデオ":http://www.youtube.com/watch?v=qnXoGnUU6uI )を GridView と ListModel で作成したものです。
このサンプルは3つのファイルから構成されています。一つがモデル(WidgetModel.qml)で、一つがデリゲート(IconItem.qml)、最後が GridView (Main.qml)です。
WidgetModel.qml
import QtQuick 1.0
ListModel {<br /> ListElement { icon: "Images/widget1.png&quot;; gridId: 0}<br /> ListElement { icon: "Images/widget2.png&quot;; gridId: 1}<br /> ListElement { icon: "Images/widget3.png&quot;; gridId: 2}<br /> ListElement { icon: "Images/widget4.png&quot;; gridId: 3}<br /> ListElement { icon: "Images/widget5.png&quot;; gridId: 4}<br /> ListElement { icon: "Images/widget6.png&quot;; gridId: 5}<br /> ListElement { icon: "Images/widget7.png&quot;; gridId: 6}<br /> ListElement { icon: "Images/widget8.png&quot;; gridId: 7}<br /> ListElement { icon: "Images/widget9.png&quot;; gridId: 8}<br />}
IconItem.qml
import QtQuick 1.0
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: "#326487&quot;; border.width: 6<br /> color: "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; 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 }<br /> }<br /> transitions: Transition { NumberAnimation { property: "scale&quot;; duration: 200} }<br /> }<br /> }<br />}
Main.qml
import QtQuick 1.0
Rectangle {<br /> width: 640; height: 480<br /> color: "#222222&quot;<br /> GridView {<br /> id: grid<br /> interactive: false<br /> anchors {<br /> topMargin: 60; bottomMargin: 60<br /> leftMargin: 140; rightMargin: 140<br /> fill: parent<br /> }<br /> cellWidth: 120; cellHeight: 120;<br /> model: WidgetModel { id: icons }<br /> delegate: IconItem { }<br /> MouseArea {<br /> property int currentId: –1 // Original position in model<br /> property int newIndex // Current Position in model<br /> property int index: grid.indexAt(mouseX, mouseY) // Item underneath cursor<br /> id: loc<br /> anchors.fill: parent<br /> onPressAndHold: currentId = icons.get(newIndex = index).gridId<br /> onReleased: currentId = –1<br /> onMousePositionChanged:<br /> if (loc.currentId [[Image:= -1 && index |= -1 && index ]]= –1 && index != newIndex)<br /> icons.move(newIndex, newIndex = index, 1)<br /> }<br /> }<br />}
ビデオ
[YouTubeID:4tt5hQYS4Fo]
関連するフォーラムのスレッド:
"iOS Style Rearrange of Icons":http://developer.qt.nokia.com/forums/viewthread/2327/