Passing data between QML pages/files?
Hi all,
I am looking for some beginners advice here.
I have a number of lists being presented to the user. Each list is built from an xml feed using xmlListModel and listView, and each list is on a separate QML page. So, when the user makes a selection from the first list, her selection should be available to the code on Page2.qml where it will be used to build the next list (e.g. If I click ''fruits'', the next page is going to load fruits.xml to build a listView).
My problem is that I can't seem to pass the information associated with a selection to the next page. How can I access the data associated with my first listModel?
(as an aside, links to third party tutorials that you think might help are appreciated)
Re: Passing data between QML pages/files?
it will be great if you tell us the ways you tried.
The simplest approach would be to have signal with a parameter as the selected content from the model, on clicking any item on the listview you can fire the signal with the selected item.
Re: Passing data between QML pages/files?
I have gotten as far as getting the data I want to my MouseArea element, but don't know how to send it any further. Keep in mind I started learning QML this week... lots of reading.
Here's a simplification of my code, assume the rectangles/mouseareas are properly drawn. I have the onClicked signal where I have been able to print the data I'm after to my console and switch the page.
[CODE]
XmlListModel {
id: routeList
source: ...
XmlRole { name: "title"; query: "@title/string ()" }
}
ListView {
id: routeSelect
model: routeList
delegate: Item {
property variant routeData: model
id: delegate
Rectangle {
id: wrapper
Text {
id: routeName
text: title
}
MouseArea {
onClicked: {
routeSelect.currentIndex = index
console.log(routeSelect.currentItem.routeData.title) //the xml role "title" is coming through successfully
pageStack.push(Qt.resolvedUrl("Page2.qml"))
}
}
}
}
}
}
[/CODE]
Essentially, what I would like to do is access "routeSelect.currentItem.routeData.title" from Page2.qml, which is currently unable to find the variable.
Re: Passing data between QML pages/files?
actually this can be a wrong approach in navigation this is what is see.
You might have a stack where you might have declared your pages correct??
First what you can do is declare a property and a signal in your Page1.qml like:
[code]
property string listitemString
signal listItemClicked()
[/code]
now in the mouseArea what you can do is:
[code]
page1.listItemString = routeSelect.currentItem.routeData.title
page1.listItemClicked()
[/code]
We are assigning the value of currentItem data in listItemData Property which you can use later.
and then listItemClicked() is emitted.
in your pageStack.qml
[code]
Page1{
onListItemClicked{
page1. listItemData // you have you title here
// push page2.qml here and do whatever you want to do with the title
}
}
[/code]
I hope its now much clear to you.