I want to have single view object, single model object and one delegate (that not really matter single delegate with conditions or few specific delegates, both ways not works) object to display dynamical data (with different roles) in list form. Why im trying to implement stuff in that way in reason to make code more united and compact.
Here the code snippet: when user clicks on “button” model content changes to elements with different role names (“letters” to “numbers”), property mode set to “numbers” and delegate should use new roles, but that
not works (model filled with new data, but delegate don’t display anything).
import QtQuick 2.2
Rectangle {
id: main
width: 640
height: 480
property string mode: "letters"
ListView {
id: view
anchors.top: main.top
anchors.bottom: button.top
delegate: delegate
model: model
}
Component {
id: delegate
Text {
text: {
if (mode == "letters")
letter
if (mode == "numbers")
number
}
}
}
ListModel {
id: model
ListElement {
letter: "A"
}
ListElement {
letter: "B"
}
ListElement {
letter: "C"
}
}
Rectangle {
id: button
anchors.bottom: main.bottom
width: 100
height: 100
border.width: 1
MouseArea {
anchors.fill: parent
Text {
anchors.centerIn: parent
text: "Change"
}
onClicked: {
model.clear();
mode = "numbers";
model.append({"number":"1"});
model.append({"number":"2"});
model.append({"number":"3"});
}
}
}
}
Here the application output:
Starting C:\Qt\5.3.0\5.3\mingw482_32\bin\qmlscene.exe...
.../test.qml:25: ReferenceError: number is not defined
.../test.qml:25: ReferenceError: number is not defined
.../test.qml:25: ReferenceError: number is not defined
.../test.qml:25: ReferenceError: number is not defined
.../test.qml:25: ReferenceError: number is not defined
.../test.qml:25: ReferenceError: number is not defined
↧








