动态视图排列示例中的索引未更新

时间:2016-08-03 06:14:06

标签: qt qml qt5

我读到了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
            }

但是,我意识到在重新排列元素之后,索引值在UI中显示为相同。指数是否没有变化?或者文本的绑定工作不正常,重新排列后更新的索引值没有显示?

之前:

enter image description here

稍微向下移动最顶层的元素(Polly)后:

enter image description here

我理解它的方式,当我们从上到下移动时,Ranks应始终显示从0到最高值,而不管重新排列,因为索引值将按此顺序排列。

1 个答案:

答案 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 }
}