QML, dynamically adding elements to a listview

Hairo picture Hairo · Jan 21, 2013 · Viewed 16k times · Source

I need some help with adding elements into a qml listview, i have a textarea and a button that will add the textarea text into a listview item when is pressed, here's my attempt:

Component {
    id: delegate
    Item {
        width: 200; height: 28
        Label {
            text: score
        }
    }
}

ListView {
     id: p1scores
     model: p1model
     delegate: delegate
     anchors.top: p1name.bottom
     anchors.topMargin: units.gu(1)
}

ListModel {
     id: p1model
     ListElement { score: "0" }
}

TextArea {
     id: p1input
     width: units.gu(8)
     height: units.gu(3)
     horizontalAlignment: TextEdit.AlignHCenter
     inputMethodHints: Qt.ImhDigitsOnly
     contentHeight: units.gu(60)
     anchors.topMargin: units.gu(8)
}

Button {
     id:p1button
     text: i18n.tr("Add")
     width: units.gu(8)
     onClicked: {
        p1model.append({"score": p1input.text})
        p1input.text = ""
     }
}

i tried appending it but doesn't shows up in the listview... any help?

Answer

Patrick picture Patrick · Sep 20, 2013

Try without quotes around 'score', like this:

onClicked: {
    p1model.append({score: p1input.text})
    p1input.text = ""
}