Reputation: 742
How do you get data that is inside listview. I have a QML listview that has a Textfield inside of the delagate. How should i call the text in the textfield inside the delagate of the listview?
ListView {
id:listview
model: 10
delegate: Component{
id:component
Item{
id:item
Textfield{
id:textfield
}
}
}
Button{
onClicked:{
for(var i=0 ; i<listview.model; i++){
myfunc( ) <---- textfield at index i text to be put here
}
}
}
Upon pressing the button i would like to get the data in the textfield at specific indexes of the listview or in the example code all.
Upvotes: 2
Views: 4020
Reputation: 244301
Your approach is incorrect, you must access the data through the model, but your model is read-only, that is, it will never be modified, instead you must use a ListModel or a QAbstractItemModel.
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ListModel{
id: mymodel
Component.onCompleted: {
for(var i=0; i< 10; ++i){
mymodel.append({"text": ""})
}
}
}
ListView {
id:listview
height: contentHeight
model: mymodel
delegate: TextField{
id:textfield
onTextChanged: model.text = text
}
}
Button{
text: "Press me"
anchors.top: listview.bottom
onClicked:{
for(var i=0 ; i<mymodel.count; i++){
console.log(mymodel.get(i).text) // <---- textfield at index i text to be put here
}
}
}
}
Upvotes: 2