2011-09-13 28 views
4

は、私が試した:qmlグリッドの要素を入れ替える方法は?

var child = grid.children[slot1]; 
grid.children[slot1] = grid.children[slot2]; 
grid.children[slot2] = child; 

しかし、それは動作しませんでした。

要素をグリッド(element.parent = grid)に親子変更すると効果的に追加されますが、並べ替える方法はありません。

私自身のQMLウィジェットを使うべきですか?

答えて

7

ListModelは可動要素の機能を提供し、GridView(グリッドだけでなく)と共に使用することができます。あなたは、デリゲートにアニメーションを追加したい場合は

GridView { 
    id: grid 
    anchors.fill: parent 
    cellWidth: 32 
    cellHeight: 32 
    property bool loaded: false; 

    model : ModelInheritingListModel { 
    } 

    delegate: MyDelegate { 

    } 

    onSwap: { 
     var min = Math.min(slot1, slot2); 
     var max = Math.max(slot1, slot2); 
     grid.model.move(min, max, 1); 
     grid.model.move(max-1, min, 1); 
    } 
} 

そして、スワップとき:

Item { 
    id: main 
    width: grid.cellWidth 
    height: grid.cellHeight 

    Image { 
     id: img 
     x: main.x 
     y: main.y 

     /* In order to use animations, we can't use the main level component as 
      it technically is the same item, so we need to animate the image. 

      So we set the image's position relative to the parent instead, so 
      we can animate its coordinates properly */ 
     parent: grid 

     Behavior on x { NumberAnimation { duration: 400; easing.type: Easing.InOutCubic}} 
     Behavior on y { NumberAnimation { duration: 400; easing.type: Easing.InOutCubic}} 

     source: imagesource 
     width: 32 
     height: 32 
    } 
} 

お知らせあなたが親を変更して、使用する必要がありトリック...

くそーQML!

関連する問題