How to implement your own type of ListModel?



  • Hello everyone,

    What I've been trying to do is creating my own personal ListModel.
    A normal List Model looks like this:

    @ListModel{
    id: myModel
    ListElement{ name:"Joe"; title: "President"}
    }@

    But I would like to add more functionality to that model and I thought of using a C++ class binding with the QML.

    Right now I have my own type of model, something like this:

    @
    MyModel
    {
    id: myModel
    nodes : [
    Node{name:"root"},
    Node{name:"child"}]
    }
    @

    But I would like to skip the "nodes" property so the syntax could be the same as in the ListModel, let's say something like this:
    @
    MyModel
    {
    Node{name:"root"}
    Node{name:"child"}
    }
    @

    Right now I am using the class QQmlListProperty, but I don't know if this class could help me do what I want. I feel like I am getting closer but hopefully someone could share some knowledge.

    Thanks in advance!



  • Wouldn't it be enough to create a model in Cpp and pass it to QML? If it would be enough i could help you with the Cpp model but i haven't worked with creating an own QML model from QML so i might not help you with that.



  • I know how to create a model in C++ and pass it to QML and actually I am using C++ bindings with QML.
    I would like to do the mentioned in my post to be close to the syntax of a usual ListModel in QML, so people could follow those rules with my own Model.

    The only thing different from what I want, and I would like to remove, is that "nodes" property in MyModel.

    Thanks for your response!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.