[GH-ISSUE #59] Returning a List<Type> from a Model #28

Closed
opened 2026-05-05 11:00:07 -06:00 by gitea-mirror · 4 comments
Owner

Originally created by @MaxMommersteeg on GitHub (Aug 13, 2018).
Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/59

I'm trying to build a small application that displays todo items in a qml view. To do this I want to return a
List<TodoItem> from my model.

Problem

I'm not able to iterate over the list using a for-loop. console.log() displays the following value: List`1(0x1c071c50b30).

I'm also not able to iterate over the list.

Model

public class TodoItemListModel
{
    private readonly List<TodoItem> _todoItems;

    public TodoItemListModel()
    {
        _todoItems = new List<TodoItem>
        {
            new TodoItem("Cleaning the house"),
            new TodoItem("Doing groceries"),
            new TodoItem("Getting a haircut")
        };
    }

    public List<TodoItem> GetTodoItems()
    {
        return _todoItems;
    }
}

QML

Page {
        ListView {
                model: listViewModel
                Component.onCompleted: {
                        var arrTodoItems = ctrl.getTodoItems()
                        console.log(arrTodoItems)
                        for (var i = 0; i < arrTodoItems.length; i++)
                        {
                                console.log(arrTodoItems[i])
                        }
                }
        }

        TodoItemListModel {
                id: model
        }
}

Not sure if this isn't supported yet or something that I'm doing incorrectly.

Originally created by @MaxMommersteeg on GitHub (Aug 13, 2018). Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/59 I'm trying to build a small application that displays todo items in a qml view. To do this I want to return a `List<TodoItem>` from my model. ### Problem I'm not able to iterate over the list using a for-loop. `console.log()` displays the following value: ```List`1(0x1c071c50b30)```. I'm also not able to iterate over the list. ### Model ```csharp public class TodoItemListModel { private readonly List<TodoItem> _todoItems; public TodoItemListModel() { _todoItems = new List<TodoItem> { new TodoItem("Cleaning the house"), new TodoItem("Doing groceries"), new TodoItem("Getting a haircut") }; } public List<TodoItem> GetTodoItems() { return _todoItems; } } ``` ### QML ```csharp Page { ListView { model: listViewModel Component.onCompleted: { var arrTodoItems = ctrl.getTodoItems() console.log(arrTodoItems) for (var i = 0; i < arrTodoItems.length; i++) { console.log(arrTodoItems[i]) } } } TodoItemListModel { id: model } } ``` Not sure if this isn't supported yet or something that I'm doing incorrectly.
Author
Owner

@pauldotknopf commented on GitHub (Aug 14, 2018):

Yeah, this is a sore point at the moment. This is the next thing I'll be working on (#55).

Until then, create a method on your model to get an item from a list at an index.

pubic class Model
{
    public object GetItem(IList<object> list, int index)
    {
        return list[index]
    }
}
<!-- gh-comment-id:412725147 --> @pauldotknopf commented on GitHub (Aug 14, 2018): Yeah, this is a sore point at the moment. This is the next thing I'll be working on (#55). Until then, create a method on your model to get an item from a list at an index. ``` pubic class Model { public object GetItem(IList<object> list, int index) { return list[index] } } ```
Author
Owner

@MaxMommersteeg commented on GitHub (Aug 14, 2018):

Alright, thanks for your explanation. Perhaps we can add some context of this issue to issue https://github.com/pauldotknopf/Qml.Net/issues/55?

<!-- gh-comment-id:412899782 --> @MaxMommersteeg commented on GitHub (Aug 14, 2018): Alright, thanks for your explanation. Perhaps we can add some context of this issue to issue https://github.com/pauldotknopf/Qml.Net/issues/55?
Author
Owner

@pauldotknopf commented on GitHub (Aug 15, 2018):

There is a lot of moving parts to that issue, and I haven't had time to fully suss out the reqs.

I've been looking at it now, and I think the first thing to address is #31. I will likely have this done by this weekend.

<!-- gh-comment-id:413283161 --> @pauldotknopf commented on GitHub (Aug 15, 2018): There is a lot of moving parts to that issue, and I haven't had time to fully suss out the reqs. I've been looking at it now, and I think the first thing to address is #31. I will likely have this done by this weekend.
Author
Owner

@pauldotknopf commented on GitHub (Aug 19, 2018):

This feature is completed.

See this commit for an example: 551dd105c8

<!-- gh-comment-id:414101028 --> @pauldotknopf commented on GitHub (Aug 19, 2018): This feature is completed. See this commit for an example: https://github.com/pauldotknopf/Qml.Net.Examples/commit/551dd105c8bdef6e369fa7d058cddd9d2afccca5
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/qmlnet#28
No description provided.