Access model from outside the delegate
-
Hi,
I have a listview, a Delegate and a c++ AbstractItemModel. I have a side panel at the right of the listview. When I click on a item I wants to set a binding between current model index selected and the right panel.
The dirty methods is to embed the right panel as child in the delegate of the listview but it's seems useless that each item draw has a right side panel. So I need to be able to access the model and the current Index outside of the listview. I have tryed using "http://lists.qt.nokia.com/pipermail/qt-qml/2010-December/001830.html" but it don't works at all. -
My understanding:
You just want to change the ListView's currentItem when you select an item from the right of the Listview.You have something like:
@ListView {
id: myList
model: myAbstractModelFromC
}@There are a few ways to do this:
@
myList.currentItem = panel.currentItem
@
OR
@
MyPanel {
property alias currItem: myList.currentItem
currentItem: currItem
}
@
OR
@
MyPanel {
currentItem: myList.currentItem
}
@You can have the 'currentItem' variable in a global property if need be.
Am I misunderstanding something? -
Hi,
here an example : (sorry ... it's more easier to understand :)
@import QtQuick 1.0Rectangle {
width: 600; height: 480
Component {
id:deleg
Row {
Rectangle {
width: 100; height: 30
Text {
anchors.fill: parent
text: model.label
}
MouseArea {
anchors.fill: parent
onClicked: {
listview.currentIndex=index;
}
}
}
}
}ListView { id: listview model: externalmodel delegate: deleg } Rectangle { id: extrapanel anchors.left: listview.right Text { id: extrapaneltext text: listview.model.currentItem.label } }
}@
In the delegate I can access without trouble my model, but I wants to have a binding between data in model of the currentItem in extrapanel (Imagine extrapanel is an heavy component with a lot of things). For now the only solution I have find is to add extrapanel in the delegate, change visibility to true of the currentIndex (and false for other) and reparent the extrapanel to the mainpanel to set the position ....
I Imagine that there is another way and it seems it's a quite common situation : you have a list and a model, when you select an item you wants to bind the current index to the panel of the right.
I hopes it's more clear ...
-
Still not clear for me :
Why can't you access it form extrapanel?By the way, what's with your text property? The model doesn't have any 'current' properties.
It should be something like:
@text: listview.model.get(listview.currentIndex).label@
Is that what you had trouble with? -
Either in the extrapanel:
@Binding { target: extrapaneltext; property: "text"; value: listview.model.get(listview.currentIndex).label }@Or in the item:
@Binding { target: extrapaneltext; property: "text"; value: label; when: list.ListView.isCurrentItem }@See bottom of this page: http://doc.qt.nokia.com/4.7-snapshot/qml-binding.html
-
The documentation definitely has a 'get' function for ListModel (http://doc.qt.nokia.com/4.7-snapshot/qml-listmodel.html).
Maybe the QAbstractListModel doesn't expose the same function then.
You may need to code it in (C++ side).My second example (within item) should still work for you without needing a get function.