listmodels

81
3
Jump to solution
12-05-2018 09:46 AM
Highlighted
New Contributor II

In the AppStudio Sample "Find A Route", I want to extract the text of the Directions, that are in the directionListModel of the l

ListView.

I assume it if fairly simple, and I have tried a buch of things, but I cannot get it - any quick suggestions

e.g., I tried

for(var i = 0; i < directionListModel.count; ++i) {
 console.log(directionListModel.get(i).text);
}

I get

TypeError: Cannot read property 'count' of null

I put this at the end of onClicked in the "Solve Route" button

thanks

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted

At the end of onClicked, solveRoute() is being called, but this is an asynchronous task. What you need to do is listen for the tasks status to change. There is already a handler for this not too far below the button in the routeTask object. Put your code into the onSolveRouteTaskStatusChanged block after the directionListModel is filled. At this point, the task will be completed and the data won't be null.

Hope this helps,

Keith

View solution in original post

3 Replies
Highlighted

At the end of onClicked, solveRoute() is being called, but this is an asynchronous task. What you need to do is listen for the tasks status to change. There is already a handler for this not too far below the button in the routeTask object. Put your code into the onSolveRouteTaskStatusChanged block after the directionListModel is filled. At this point, the task will be completed and the data won't be null.

Hope this helps,

Keith

View solution in original post

Highlighted
Regular Contributor

As Keith mentioned, it is asynchronous, you need to wait for the results to be ready before you can view it. Try adding the code under the onClicked for the directions button

Another thing is that the property of DirectionManeuver::directionText to get the directions and not just the text

 MouseArea {
                        anchors.fill: parent
                        onPressed: directionButton.pressed = true
                        onReleased: directionButton.pressed = false
                        onClicked: {
                            // Show the direction window when it is clicked
                            translate.x = directionWindow.visible ? 0 : (directionWindow.width * -1);
                            directionWindow.visible = !directionWindow.visible;
                            for(var i = 0; i < directionListModel.count; ++i) {
                             console.log(directionListModel.get(i).directionText );
                            }
                        }

Nakul

Highlighted
New Contributor II

and then there's asynchronous..so the lesson is wait until the data is ready!!

Perfect. That worked - thanks for the quick response

to be specific, I used:

 console.log(directionListModel.count);
console.log(directionListModel.get(1).directionText)

and got

qml: 12

qml: Go east on UPAS ST toward ALBERT ST

fun stuff!

Reply
0 Kudos