Accessing element in a separated Listmodel file
-
Hi,
I do not manage to access a given element from a Listmodel located in a separated file.What works is having the Listmodel inside the main file:
main.qml:
ListModel { id: taskData ListElement { name : "name1" } ListElement { name : "name2" } } Column { id: column Repeater { model: taskData Text { text: "I'm item " + taskData.get(1).name } } }
It gives the following output:
I'm item name2
I'm item name2What I do not manage to access an element if the Listmodel in an external file.
TaskData.qml
import QtQuick 2.0 ListModel { id: taskData property alias taskData: taskData ListElement { name : "name1" } ListElement { name : "name2" } }
main.qml
Window { visible: true width: 200 height: 300 Column { id: column Repeater { model: TaskData {} Text { text: "I'm item " + taskData.get(1).name } } } }
When running the second example I get
qrc:/main.qml:20: ReferenceError: taskData is not defined
qrc:/main.qml:20: ReferenceError: taskData is not definedWhat should I do to be able to read taskData if it is present in an external file ?
Thanks in advance,
Emmanuel -
-
@Allon said in Accessing element in a separated Listmodel file:
Text { text: "I'm item " + taskData.get(1).name }
You don't need to provide an id and to to create a property for your listModel component,
try instead to access the listmodel data directly with its role :Text { text: "I'm item " + name }
you can disambiguate a role name using modelData (i.e modelData.name).
-
@Charby Hi,
Is it not only the case when I am in the repeater?In my example I skimmed a large project where I am getting an index from a mousearea drag and drop and I want to access the element pointed by this index.
Here is the original line of my program:
taskData.get(taskColumnRectangleIndex).tasks.insert(taskData.get(taskColumnRectangleIndex).tasks.count, {"description": "task number: " + taskData.get(taskColumnRectangleIndex).tasks.count, /"taskHovered": true/ color:"red"})
However in order to make the code a little bit more readable I wanted to move my Listmodel taskData out of the file I al coding in.
Do you have an idea how I could then access taskData if it is located in a separated file?
Thanks in advance,
Emmanuel
-