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. QML Direct fetch a model from C++
Forum Updated to NodeBB v4.3 + New Features

QML Direct fetch a model from C++

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 2 Posters 2.0k Views 2 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.
  • K Offline
    K Offline
    Kofr
    wrote on last edited by Kofr
    #1

    How can I make something like this:

    ListView {
      ...
      model: model.getModel();
    }
    

    If I for example have a Model which consists of Models. Or can I use setContextProperty for class with Models??

    p3c0P 1 Reply Last reply
    0
    • K Kofr

      How can I make something like this:

      ListView {
        ...
        model: model.getModel();
      }
      

      If I for example have a Model which consists of Models. Or can I use setContextProperty for class with Models??

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @Kofr Yes Ofcourse if using C++ models

      • You can return a C++ model and assign it to model property
      • You can use setContextProperty to set C++ models and then access from QML
        See http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html for more info.

      157

      K 1 Reply Last reply
      1
      • p3c0P p3c0

        @Kofr Yes Ofcourse if using C++ models

        • You can return a C++ model and assign it to model property
        • You can use setContextProperty to set C++ models and then access from QML
          See http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html for more info.
        K Offline
        K Offline
        Kofr
        wrote on last edited by
        #3

        @p3c0 thx,
        I need to have dinamically creating datamodels inside head Datamodel. What approach can I consider to pick up? so far I see only using QObjectList models but it will make me having to rewrite much code.

        p3c0P 1 Reply Last reply
        0
        • K Kofr

          @p3c0 thx,
          I need to have dinamically creating datamodels inside head Datamodel. What approach can I consider to pick up? so far I see only using QObjectList models but it will make me having to rewrite much code.

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @Kofr If I understood you correctly: you can keep a class Datamodel and set it as a context property name model and this class will contain a Q_INVOKABLE function getModel and make it return you C++ model instance.

          157

          K 1 Reply Last reply
          0
          • p3c0P p3c0

            @Kofr If I understood you correctly: you can keep a class Datamodel and set it as a context property name model and this class will contain a Q_INVOKABLE function getModel and make it return you C++ model instance.

            K Offline
            K Offline
            Kofr
            wrote on last edited by
            #5

            @p3c0 yeah something like from QML

            getModel(2);
            

            means that I want to get nested DataModel like this

            QList <QAbstactListModelInheritedModel> List; 
            
            return List.at(2);
            

            Shall I create a function which casts my model to QObject and returns this QObject?

            p3c0P 1 Reply Last reply
            0
            • K Kofr

              @p3c0 yeah something like from QML

              getModel(2);
              

              means that I want to get nested DataModel like this

              QList <QAbstactListModelInheritedModel> List; 
              
              return List.at(2);
              

              Shall I create a function which casts my model to QObject and returns this QObject?

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @Kofr Yes it should work. May be require a list of pointers. Try out.

              Shall I create a function which casts my model to QObject and returns this QObject?

              Not needed. model property will understand any class subclassed from QAbstractItemModel or its derivatives.

              157

              1 Reply Last reply
              0
              • K Offline
                K Offline
                Kofr
                wrote on last edited by
                #7

                HELPED ME A LOT
                http://stackoverflow.com/questions/29558138/use-a-qabstractlistmodel-in-another-one

                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