How to load a c++ treeModel in QML TreeView dynamicaly



  • Hello guys ! I just started to deal with QML and I have been kind of stuck with qml treeViews.

    Here is what I have :

    I have a working treeModel in C++ ( with the QAbstractItemModel class )

    I have this line in the main.cpp wich ,I assume, allow my qml file to find it :

    engine.rootContext()->setContextProperty("currentTreeModel",myTreeModel);
    

    Than I construct my treeView with :

    TreeView {
    
            id : treeView
            anchors.fill: parent
            model : currentTreeModel;
    
            TableViewColumn {
                role: "role"
                title: "title"
            }
    }
    

    It's working good and I get a nice treeView

    Here is the problem :

    I want my model to be rebuilt each time the user pick a new kind of data and I have no clue how to do so ...

    What I have tried :

    I have tried to make a function that return me a treeModel and make something like

    TreeView {
    
            id : treeView
            anchors.fill: parent
    ////////////////////////////////////
            model  : getModel();
    ////////////////////////////////////
            TableViewColumn {
                role: "role"
                title: "title"
            }
    }
    

    with a c++ function like that :

    Q_INVOKABLE TreeModel getModel()
    {
      . . .
      . . .
      . . .
      return newTreeModel;
    }
    

    but it won't compile because a model cannot be copied

    Do someone know how to deal with it or at least have any clue ?
    Is it somehow possible to "redeclare" to the engine a new model ?

    Thank you for helping


  • Moderators

    @buckTarle
    are the models similiar regarding their structure?
    Basically it's simply enough to reset the model and fill it with new data.



  • Hmmm not sure about what do you mean by similar.

    They should be quite similar in fact all item are based on the same C structure so they have the same roles.
    But they do not have the same tree-form and not the same lenght.


  • Moderators

    @buckTarle said in How to load a c++ treeModel in QML TreeView dynamicaly:

    They should be quite similar in fact all item are based on the same C structure so they have the same roles.

    yes that was what i meant.
    Then simply update your model by doing the following and your qml view also updates:

    void MyModel::refreshData()
    {
         this->beginResetModel();
         // remove old data and re-initialize new data here
         this->endResetModel();
    }
    


  • Ok I'm going to give it a try ! Thank you very much.



  • ok so i finaly got it to work with tons of tries !

    I implemented the refreshData() method but I have had hard time figuring out how to pass the data needed for the new model.

    I end up declaring the TreeModel as a static atribute using a static member of another class I use in my project. The other class is Registered in the main.cpp like that :

        qmlRegisterType<MyClass>("com.myself", 1, 0, "MyClass");
    
    
    class MyClass : public QObject
    {
        Q_OBJECT
    public
        Q_INVOKABLE void reloadModel();
        Q_INVOKABLE TreeModel* getModel();
    private :
        static TreeModel* m_currentModel;
    

    The first call to the model will allocate one and than I am sure to get the same pointer every time I want to modify data in it.

    I'm not sure this is the cleanest way to go but It's working for now !

    Thank for helping raven-worx


  • Moderators

    @buckTarle said in How to load a c++ treeModel in QML TreeView dynamicaly:

    I'm not sure this is the cleanest way to go but It's working for now !

    yes, thats actually perfect for shared models.


Log in to reply
 

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