2016-08-03 82 views
1

我閱讀了關於dynamically re-arrange-able view。 Qt-Creator也提供了相同的例子。索引在動態視圖排列示例中未更新

我想在我的數據庫中存儲更新後的重新排列順序。我添加一個額外的行的列定義用於檢查索引值:

 Column { 
       id: column 
       anchors { fill: parent; margins: 2 } 

       Text { text: 'Name: ' + name } 
       Text { text: 'Type: ' + type } 
       Text { text: 'Age: ' + age } 
       Text { text: 'Size: ' + size } 
       Text { text: 'Rank: ' + index } // Added to test the index values 
      } 

然而,我認識到重新排列的元素後,索引值被示出在用戶界面相同。索引不變嗎?或者文本的綁定無法正常工作,更新後的索引值在重新排列之後沒有顯示出來?

前:

enter image description here

移動最上面的元素(波利)一點點下來之後:

enter image description here

我的理解是,排名應該始終從0露面到最高價值,因爲我們從上到下移動,無論重新排列,因爲索引值將按照這個順序排列。

回答

1

事情是您在代表中顯示的indexPetModel中的原始索引。爲了說服你,你只需稍微修改一下這個例子。

PetsModel { 
    id: petsModel 
} 

DelegateModel { 
    id: visualModel 
    model: petsModel 
    delegate: dragDelegate 
} 

... 

ListView { 
    id: view 
    anchors { fill: parent; margins: 2; rightMargin: parent.width/2+2 } 
    model: visualModel 
    spacing: 4 
} 

ListView { 
    id: view2 
    anchors { fill: parent; margins: 2; leftMargin: parent.width/2+2 } 
    model: petsModel 
    delegate:dragDelegate 
    spacing: 4 

} 

你會看到,無論你怎麼走動的項目中view,實際項目中view2順序保持不變。

DelegateModel充當代理,以實際PetModel(有點像QSortFilterProxyModel

如果你想顯示的代理指標,使用附帶的dragArea.DelegateModel.itemsIndex財產

Column { 
    id: column 
    anchors { fill: parent; margins: 2 } 

    Text { text: 'Name: ' + name } 
    Text { text: 'Type: ' + type } 
    Text { text: 'Age: ' + age } 
    Text { text: 'Size: ' + size } 
    Text { text: 'Index: ' + index } 
    Text { text: 'Display Index: ' + dragArea.DelegateModel.itemsIndex } 
}