How to access listview current item from other item?
-
can i have listview's current item model info ?
when i try some thing like this
@//:D
import QtQuick 2.0Rectangle
{
id: root;
height: 500;
width: 500;
Column
{
ListView
{
id: lv;
height: 400;
width: root.width;
clip: true;
orientation: ListView.Horizontalmodel: ListModel{ id: model; ListElement{ name :"1"; } ListElement{ name :"2"; } ListElement{ name :"3";} ListElement{ name :"4";} } delegate: com; } Text { id:txt; height:100; width:root.width; text: lv.currentItem.name; } } Component { id: com; Rectangle { id: rec height: 400; width: root.width; Text { id: rectext; anchors.centerIn: parent; text: model.name; } } }
}
@
i have this error: "Unable to assign [undefined] to QString" :D -
Hi, well you could get the warning while the list view is still populating the items, so there might be a warning because list.currentItem will be undefined at first, but it may work after that? some more info please :)
Also currentItem refers to the QML Item (delegate Item) and not the model data of that item, so you might have to use list.currentIndex, e.g. you can try
@
model.get(list.currentIndex).name
@ -
what do you mean after you change list view!?
ListView.currentIndex is not updated by itself, you have to take care of that, that might be the problem I think. read the documentation for "currentIndex", so it depends how you want to change it, by button, click or mouse hover or whatever, I can't tell you how to do that without more details about your app :) -
in simple way
(that i can explain -> i have bad english , i khnow i must improve it :) )
i want to khnow user is seeing which part of my listview
i want to have that part's name
if i change code to this
@//:D
import QtQuick 2.0Rectangle
{
id: root;
height: 500;
width: 500;
Column
{
ListView
{
id: lv;
height: 400;
width: root.width;
clip: true;
orientation: ListView.Horizontal
snapMode: ListView.SnapOneItem;// added new :Dmodel: ListModel{ id: model; ListElement{ name :"1"; } ListElement{ name :"2"; } ListElement{ name :"3";} ListElement{ name :"4";} } delegate: com; } Text { id:txt; height:100; width:root.width; text: lv.currentItem; } } Component { id: com; Rectangle { id: rec height: 400; width: root.width; Text { id: rectext; anchors.centerIn: parent; text: model.name; } } }
}
@
i want to know user is looking which item ?
tanx alot for your helping my friend
(i try my best to explain my prob
i must to stop being lazy and start learning english :| ) -
hey, I modified your code a little look at this:
@
//:D
import QtQuick 2.0Rectangle
{
id: root;
height: 500;
width: 500;
Column
{
ListView
{
id: lv;
height: 400;
width: root.width;
clip: true;
orientation: ListView.Horizontal
snapMode: ListView.SnapOneItem;// added new :Dmodel: ListModel{ id: model; ListElement{ name :"1"; } ListElement{ name :"2"; } ListElement{ name :"3";} ListElement{ name :"4";} } delegate: com; } Text { id:txt; height:100; width:root.width; text: lv.currentItem.name; // now you can use currentItem.name to access the property of the delegate } } Component { id: com; Rectangle { id: rec property alias name: rectext.text // added this property alias to access the text of the current list item height: 400; width: root.width; Text { id: rectext; anchors.centerIn: parent; text: model.name; } MouseArea { anchors.fill: parent onClicked: { lv.currentIndex = index // you used listView.currentIndex here? } } } }
}
@
I've added a property to access the text and your MouseArea to the delegate, that works fine and you can use it, see my comments in the code.
Might not be perfect because it only changes the index if you click on the item, not on you drag/swipe it, but that is another problem for now I think.. maybe you find a solution to that.