QML: display children of a QAbstractItemModel node



  • Hi all,

    I have a quick question concerning QAbstractItemModel and the way to display it in QML.

    Basically, I have my own model ( subclassing QAbstractItemModel ) that kind of a tree but children of a node are from a different type.
    For instance, I would have a "root" with folders and then for one node in this root, it's children are of type "file".

    What I want to display is the root ( easy part ) in a ListView and the on a click, display a new ListView that displays the children of the node I clicked on (i.e the files within a folder ).

    I have seen some examples from Qt but they all rely on the QTreeView..it is a way to represent it but not the way I want.
    How can I access the "model" of a particular node in my model. QModelIndex has a model property and I assume it is what I want but I have not find any ways to access it from QML. I'd like to use already implemented features rather than using some special role to access the model I want if possible.

    Thanks in advance :)

    PS: I hope it is clear enough...



  • You can use Q_INVOKABLE function to get the subModel from C++.

    Something like :

    QML:

    ItemListView {
        SubListView {
             ....
             model: myModel.getSubModel(i)
             ....
        }
    }
    

    ListModel.h

    QList<ListItem> m_l;
    
    Q_INVOKABLE QObject* getSubModel(int index) {
         return m_l[index].getModel();
    }
    

    ListItem.h

    subModel* m_subModel 
    
    QObject* getModel() {
        return m_submodel;
    }
    

Log in to reply
 

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