0

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
    } 
}
Nepaluz
  • 669
  • 1
  • 12
  • 27
  • 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 Jul 10 '13 at 15:57
  • 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 Jul 10 '13 at 16:34
  • 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 Jul 10 '13 at 16:45
  • I have updated the question with a more complete code example – Nepaluz Jul 10 '13 at 17:35

3 Answers3

2

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";
    } 
}
Thomas McGuire
  • 5,308
  • 26
  • 45
  • tmcguire, thanks for the contribution. Again, i had found something similar for adding a property alias, and though this too works, the response seems out of sync! Answers the question correctly though. – Nepaluz Jul 10 '13 at 20:04
2

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.

Deadron
  • 5,135
  • 1
  • 16
  • 27
  • This does it MOST elegantly! I actually had googled earlier and got a similar suggestion but did not precede the moving with the listview id! thanks – Nepaluz Jul 10 '13 at 20:02
0

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.

Kunal
  • 3,475
  • 21
  • 14
  • Kunal, you missed the question. In my edit above I mentioned that I already set the text using java, I want to set (more appropriately change) the value of "text: name" from inside the event handler, i.e onMovementEnded – Nepaluz Jul 10 '13 at 00:39
  • ok, then you need to get index to element and update item in model – Kunal Jul 10 '13 at 03:51
  • Kunal, that was the question. How do you get the index of Text in the code above? – Nepaluz Jul 10 '13 at 09:00