I have a simple QML program which has one ListView. ListView's model and delegate are defined in a separate QML files.
//Main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
//import TheModel.qml
//import TheDelegate.qml
Window {
id: window
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ListView {
anchors.fill: parent
model: theModel
delegate: theDelegate
focus: true
}
Button{
x: 394
y: 257
text: "press me"
onPressed: theModel.append({"color":"black", "cost": 5.95, "name":"Pizza"})
}
TheDelegate{
id: theDelegate
}
TheModel{
id:theModel
}
}
then the model file
//TheModel.qml
import QtQuick 2.0
ListModel{
ListElement {
color:"red";
name: "Bill Smith"
number: "555 3264"
}
ListElement {
color:"blue";
name: "John Brown"
number: "555 8426"
}
ListElement {
color:"green";
name: "Sam Wise"
number: "555 0473"
}
}
and finally the delegate
//TheDelegate.qml
import QtQuick 2.0
Component {
Rectangle{
color: model.color
width: 100
height: 100
MouseArea{
anchors.fill: parent
onPressed: model.append({"color":"black", "cost": 5.95, "name":"Pizza"})
}
}
}
if I click on delegate's MouseArea the onPressed method will need to create one ListItem, but the issue is that I cannot access the model's function from delegate. what is confusing though, that properties are being accessed in delegate through model.
can anyone point out on the right way of doing this, say if I know that the model is a ListModel and it has append method, but delegate doesn't know that, is there a way to cast model to known type then call a method of it?