Drag and Drop within a GridView/bg: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 5: | Line 5: | ||
= Влачене и пускане на елементи в GridView = | = Влачене и пускане на елементи в GridView = | ||
Този пример ще покаже влачене и пускане на елелемeнти в ''GridView'' по подобен на iOS начин ( | Този пример ще покаже влачене и пускане на елелемeнти в ''GridView'' по подобен на iOS начин ("видео":http://www.youtube.com/watch?v=qnXoGnUU6uI ). | ||
В примера има 3 файла. Един за модела (WidgetModel.qml), един за делегата (IconItem.qml) и един за GridView (Main.qml). | В примера има 3 файла. Един за модела (WidgetModel.qml), един за делегата (IconItem.qml) и един за GridView (Main.qml). | ||
Line 11: | Line 11: | ||
'''WidgetModel.qml'''<br /><code>import QtQuick 1.0 | '''WidgetModel.qml'''<br /><code>import QtQuick 1.0 | ||
ListModel {<br /> ListElement { icon: | ListModel {<br /> ListElement { icon: "Images/widget1.png"; gridId: 0}<br /> ListElement { icon: "Images/widget2.png"; gridId: 1}<br /> ListElement { icon: "Images/widget3.png"; gridId: 2}<br /> ListElement { icon: "Images/widget4.png"; gridId: 3}<br /> ListElement { icon: "Images/widget5.png"; gridId: 4}<br /> ListElement { icon: "Images/widget6.png"; gridId: 5}<br /> ListElement { icon: "Images/widget7.png"; gridId: 6}<br /> ListElement { icon: "Images/widget8.png"; gridId: 7}<br /> ListElement { icon: "Images/widget9.png"; gridId: 8}<br />}</code> | ||
'''IconItem.qml'''<br /><code>import QtQuick 1.0 | '''IconItem.qml'''<br /><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: | 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"; border.width: 6<br /> color: "transparent"; radius: 5<br /> visible: item.state "active" | ||
} | |||
Behavior on x | Behavior on x { enabled: item.state != "active"; NumberAnimation { duration: 400; easing.type: Easing.OutBack } } | ||
Behavior on y | Behavior on y { enabled: item.state != "active"; NumberAnimation { duration: 400; easing.type: Easing.OutBack } } | ||
SequentialAnimation on rotation | SequentialAnimation on rotation { | ||
NumberAnimation | NumberAnimation { to: 2; duration: 60 } | ||
NumberAnimation | NumberAnimation { to: -2; duration: 120 } | ||
NumberAnimation | NumberAnimation { to: 0; duration: 60 } | ||
running: loc.currentId != -1 &amp;&amp; item.state != | running: loc.currentId != -1 &amp;&amp; item.state != "active" | ||
loops: Animation.Infinite; alwaysRunToEnd: true | loops: Animation.Infinite; alwaysRunToEnd: true | ||
} | |||
states: State | states: State { | ||
name: | name: "active"; 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"; duration: 200} }<br /> }<br /> }<br />}</code> | ||
'''Main.qml'''<br /><code>import QtQuick 1.0 | '''Main.qml'''<br /><code>import QtQuick 1.0 | ||
Rectangle {<br /> width: 640; height: 480<br /> color: | Rectangle {<br /> width: 640; height: 480<br /> color: "#222222"<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 />}</code> | ||
'''Video'''<br />[YouTubeID:4tt5hQYS4Fo] | '''Video'''<br />[YouTubeID:4tt5hQYS4Fo] | ||
Теми от форума:<br /> | Теми от форума:<br />"iOS Style Rearrange of Icons":http://developer.qt.nokia.com/forums/viewthread/2327/<br />"QML and Drag and Drop":http://developer.qt.nokia.com/forums/viewthread/2426/ |
Revision as of 06:34, 25 February 2015
Тази статия е създадена заради няколко теми във форума (линковете са в края на статията), в които се пита за такъв метод. Чувствайте се свободни да добавяте коментари и код.
Влачене и пускане на елементи в GridView
Този пример ще покаже влачене и пускане на елелемeнти в GridView по подобен на iOS начин ("видео":http://www.youtube.com/watch?v=qnXoGnUU6uI ).
В примера има 3 файла. Един за модела (WidgetModel.qml), един за делегата (IconItem.qml) и един за GridView (Main.qml).
WidgetModel.qml
import QtQuick 1.0
ListModel {<br /> ListElement { icon: "Images/widget1.png"; gridId: 0}<br /> ListElement { icon: "Images/widget2.png"; gridId: 1}<br /> ListElement { icon: "Images/widget3.png"; gridId: 2}<br /> ListElement { icon: "Images/widget4.png"; gridId: 3}<br /> ListElement { icon: "Images/widget5.png"; gridId: 4}<br /> ListElement { icon: "Images/widget6.png"; gridId: 5}<br /> ListElement { icon: "Images/widget7.png"; gridId: 6}<br /> ListElement { icon: "Images/widget8.png"; gridId: 7}<br /> ListElement { icon: "Images/widget9.png"; 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"; border.width: 6<br /> color: "transparent"; radius: 5<br /> 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 &amp;&amp; item.state != "active"
loops: Animation.Infinite; alwaysRunToEnd: true
}
states: State {
name: "active"; 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"; duration: 200} }<br /> }<br /> }<br />}
Main.qml
import QtQuick 1.0
Rectangle {<br /> width: 640; height: 480<br /> color: "#222222"<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 />}
Video
[YouTubeID:4tt5hQYS4Fo]
Теми от форума:
"iOS Style Rearrange of Icons":http://developer.qt.nokia.com/forums/viewthread/2327/
"QML and Drag and Drop":http://developer.qt.nokia.com/forums/viewthread/2426/