0
votes

How do I access the Text.text item inside the ListView delegate from an event handler for the ListView, sample code (may have syntactic errors) below.

ListView {
    id: mainLView
    model: ListViewModel {}
    delegate: Rectangle {
      id: theRect
      Text {
         id: theText
         text: ""
      }
    }
    onMovementEnded {
      //here is where I would like to access the text in Text, e.g
      theText.text = "New Value"
    }
}

The code I have does not work since theText is NOT accessible from the event handler. how do I accomplish setting the text value?

EDIT: For completeness: I can add items to the ListView via java code (during app load), access the value(s) in the ListView inside the event through the code aka

mainLView.model.append({'name': "First Value","city":"London"});
var myValue = model.get(currentIndex).city // or name

But I still can not find a way to assign a value to the delegate Text { text:""} value.

EDIT 2 10th July Here is a more complete code example of what i am trying to achieve.

ListView {
    id: mainLView
    model: ListModel { id: mainModel}
    delegate: Rectangle {
      id: theRect
      Text {
         id: theText
         text: nameX
      }
    }
    Component.onCompleted: {
        for (var x=1; x < 99; x++) {
            var data = {'nameX': "Name: " + x, "numberX":x};
            mainModel.append(data);
        }
    }
    onMovementEnded {
        //here I want to set the value to numberX e.g (this does not work)
        theText.text = mainView.model.get(currentIndex).numberX
    } 
}
3
This question is difficult to answer because its not clear what you actually want from your example. A ListView renders a delegate for each ListModel element. Are you asking how to update every Element with a value or asking how to update a specific one? This question really seems to come down to how are you designating the element that you want to edit. Once you have given us that info its trivial to help you get its index and update it. - Deadron
Deadron, thanks for the comment. I want to update the value at the index when the movement has ended (the code above is brief but demonstrates the issue). - Nepaluz
I want to update the value at the index when the movement has ended inside the signal (and thus update the listview). My full code actually has two items to each element in the listmodel; I want to display one value when the listview is rolling and the other when it stops, but for conciseness, I whittled down the code above to just show the where I need help. Let me know if you want a more complete code example. - Nepaluz
I have updated the question with a more complete code example - Nepaluz

3 Answers

2
votes

ListView has a property currentItem that you can use to access the item at the current index. To access something from the delegate, your delegate needs to have a property in its top-level item, since only those are exposed to the outside. Something like this should work:

ListView {
    id: mainLView
    model: ListModel { id: mainModel}
    delegate: Rectangle {
      property alias text: theText.text
      Text {
         id: theText
         text: nameX
      }
    }

    onMovementEnded {
        mainLView.currentItem.text = "foo";
    } 
}
2
votes

Given your comments I think the following may do what you want.

delegate: Rectangle {
      id: theRect
      Text {
         id: theText
         text: mainLView.moving ? nameX : numberX
      }
    }

This should display one of the values when the ListView is moving and a different one when it is not.

0
votes

To assign string value to theText (Text element) in list view delegate. you can use model's attribute name.

like following. here name comes from Jason object {'name': "First Value","city":"London"});

 delegate: Rectangle {
      id: theRect
      Text {
         id: theText
         text: name // or city
      }
    }

check out this link (http://kunalmaemo.blogspot.kr/2011/03/creating-custom-listview-delegate-in.html) it will help.

BTW, to get text from delegate, you need to get it from Model, you can not get it from delegate, as delegate is reusable element in listview and its value keeps changing.