Search code examples
javaqtqmlubuntu-touch

QML Java - Accessing delegate item from event handler


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

Solution

  • 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.