Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to load a c++ treeModel in QML TreeView dynamicaly
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 2.4k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    buckTarle
    wrote on last edited by
    #1

    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

    raven-worxR 1 Reply Last reply
    0
    • B buckTarle

      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

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • B Offline
        B Offline
        buckTarle
        wrote on last edited by
        #3

        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.

        raven-worxR 1 Reply Last reply
        0
        • B buckTarle

          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.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @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();
          }
          

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • B Offline
            B Offline
            buckTarle
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • B Offline
              B Offline
              buckTarle
              wrote on last edited by buckTarle
              #6

              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

              raven-worxR 1 Reply Last reply
              0
              • B buckTarle

                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

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                @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.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved