Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Drag and Drop within a GridView/ja: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Sub-categorize)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:snippets]]<br />[[Category:Developing_with_Qt::Qt Quick]]<br />[[Category:Developing_with_Qt::Qt Quick::QML]]<br />[[Category:Developing_with_Qt::Qt Quick::Tutorial]]<br />[[Category:Japanese]]<br />[[Drag_and_Drop_within_a_GridView|English]] | '''日本語''' | [[Drag_and_Drop_within_a_GridView_Bulgarian|Български]]
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
[[Category:Snippets::QML]]
[[Category:Developing with Qt::Qt Quick::QML]]
[[Category:Developing with Qt::Qt Quick::Tutorial]]
[[Category:Japanese]]


この記事はいくつかのフォーラムのスレッド(下部にリンクしました)での質問と回答を受けて作成されたものです。コードや解説でのこの記事への貢献を歓迎します。
この記事はいくつかのフォーラムのスレッド(下部にリンクしました)での質問と回答を受けて作成されたものです。コードや解説でのこの記事への貢献を歓迎します。


= GridView でのドラッグ&amp;amp;ドロップ =
= GridView でのドラッグ&ドロップ =


このサンプルは iOS スタイルのドラッグ&amp;amp;ドロップ( &quot;参考ビデオ&amp;quot;:http://www.youtube.com/watch?v=qnXoGnUU6uI )を GridView と ListModel で作成したものです。
このサンプルは iOS スタイルのドラッグ&ドロップ( [http://www.youtube.com/watch?v=qnXoGnUU6uI 参考ビデオ] )を GridView と ListModel で作成したものです。


このサンプルは3つのファイルから構成されています。一つがモデル(WidgetModel.qml)で、一つがデリゲート(IconItem.qml)、最後が GridView (Main.qml)です。
このサンプルは3つのファイルから構成されています。一つがモデル(WidgetModel.qml)で、一つがデリゲート(IconItem.qml)、最後が GridView (Main.qml)です。


'''WidgetModel.qml'''<br /><code>import QtQuick 1.0
'''WidgetModel.qml'''
<code>import QtQuick 1.0


ListModel {<br /> ListElement { icon: &quot;Images/widget1.png&amp;quot;; gridId: 0}<br /> ListElement { icon: &quot;Images/widget2.png&amp;quot;; gridId: 1}<br /> ListElement { icon: &quot;Images/widget3.png&amp;quot;; gridId: 2}<br /> ListElement { icon: &quot;Images/widget4.png&amp;quot;; gridId: 3}<br /> ListElement { icon: &quot;Images/widget5.png&amp;quot;; gridId: 4}<br /> ListElement { icon: &quot;Images/widget6.png&amp;quot;; gridId: 5}<br /> ListElement { icon: &quot;Images/widget7.png&amp;quot;; gridId: 6}<br /> ListElement { icon: &quot;Images/widget8.png&amp;quot;; gridId: 7}<br /> ListElement { icon: &quot;Images/widget9.png&amp;quot;; gridId: 8}<br />}</code>
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}
}</code>


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


'''Main.qml'''<br /><code>import QtQuick 1.0
'''Main.qml'''
<code>import QtQuick 1.0


Rectangle {<br /> width: 640; height: 480<br /> color: &quot;#222222&amp;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 &amp;&amp; index |= -1 &amp;&amp; index ]]= –1 &amp;&amp; index != newIndex)<br /> icons.move(newIndex, newIndex = index, 1)<br /> }<br /> }<br />}</code>
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 [[Image:= -1 && index |= -1 && index ]]= –1 && index != newIndex)
icons.move(newIndex, newIndex = index, 1)
}
}
}</code>


'''ビデオ'''<br />[YouTubeID:4tt5hQYS4Fo]
'''ビデオ'''
https://www.youtube.com/watch?v=4tt5hQYS4Fo


関連するフォーラムのスレッド:<br />&quot;iOS Style Rearrange of Icons&amp;quot;:http://developer.qt.nokia.com/forums/viewthread/2327/
関連するフォーラムのスレッド:
[http://developer.qt.nokia.com/forums/viewthread/2327/ iOS Style Rearrange of Icons]

Latest revision as of 13:15, 28 November 2016

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

この記事はいくつかのフォーラムのスレッド(下部にリンクしました)での質問と回答を受けて作成されたものです。コードや解説でのこの記事への貢献を歓迎します。

GridView でのドラッグ&ドロップ

このサンプルは iOS スタイルのドラッグ&ドロップ( 参考ビデオ )を GridView と ListModel で作成したものです。

このサンプルは3つのファイルから構成されています。一つがモデル(WidgetModel.qml)で、一つがデリゲート(IconItem.qml)、最後が 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

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

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 [[Image:= -1 && index |= -1 && index ]]= 1 && index != newIndex)
 icons.move(newIndex, newIndex = index, 1)
 }
 }
}

ビデオ https://www.youtube.com/watch?v=4tt5hQYS4Fo

関連するフォーラムのスレッド: iOS Style Rearrange of Icons