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. Can I expose a C++ model as Q_Property?

Can I expose a C++ model as Q_Property?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 5 Posters 1.9k 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.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by
    #1

    Hi,

    I have a QAbstractListModel which is member of a QObject class. Can I expose the model as a Q_Property to QML?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Yes, here is some help: https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html.

      (Z(:^

      1 Reply Last reply
      4
      • M Offline
        M Offline
        maxwell31
        wrote on last edited by maxwell31
        #3

        my problem here is the following:

        I write my custom plotting component (a QQuickItem). I might have several instances of these plots in my application, and each object has its own model, e.g. for the x-ticks.

        I guess using

        ctxt->setContextProperty("myModel",_tickModel);
        

        will not work as I will have several instances, and the name would then not be unique. Doing

            qmlRegisterType<TickModel>(uri, 1, 0,
                    "TickModel");
        

        would allow me to create the model in qml, but it should be created in C++. Would using qmlRegisterType allow me to do something like this?:

        class Plot : public QQuickItem {
            Q_OBJECT
        public:
         Q_INVOKABLE TickModel getTickModel()
        
        KroMignonK Gojir4G 2 Replies Last reply
        0
        • M maxwell31

          my problem here is the following:

          I write my custom plotting component (a QQuickItem). I might have several instances of these plots in my application, and each object has its own model, e.g. for the x-ticks.

          I guess using

          ctxt->setContextProperty("myModel",_tickModel);
          

          will not work as I will have several instances, and the name would then not be unique. Doing

              qmlRegisterType<TickModel>(uri, 1, 0,
                      "TickModel");
          

          would allow me to create the model in qml, but it should be created in C++. Would using qmlRegisterType allow me to do something like this?:

          class Plot : public QQuickItem {
              Q_OBJECT
          public:
           Q_INVOKABLE TickModel getTickModel()
          
          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

          @maxwell31 said in Can I expose a C++ model as Q_Property?:

          Would using qmlRegisterType allow me to do something like this?:
          class Plot : public QQuickItem {
          Q_OBJECT
          public:
          Q_INVOKABLE TickModel getTickModel()

          qmlRegisterType() will allow you to create a new instance of the given class. If you don't need to create an instance I would suggest you to do it like this:

          class Plot : public QQuickItem {
              Q_OBJECT
          public:
           Q_INVOKABLE QObject* getTickModel();
          

          But be aware that if the TickModel object did not have a parent, QML engine will take ownership and delete it when QML did not need it anymore.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          4
          • M maxwell31

            my problem here is the following:

            I write my custom plotting component (a QQuickItem). I might have several instances of these plots in my application, and each object has its own model, e.g. for the x-ticks.

            I guess using

            ctxt->setContextProperty("myModel",_tickModel);
            

            will not work as I will have several instances, and the name would then not be unique. Doing

                qmlRegisterType<TickModel>(uri, 1, 0,
                        "TickModel");
            

            would allow me to create the model in qml, but it should be created in C++. Would using qmlRegisterType allow me to do something like this?:

            class Plot : public QQuickItem {
                Q_OBJECT
            public:
             Q_INVOKABLE TickModel getTickModel()
            
            Gojir4G Offline
            Gojir4G Offline
            Gojir4
            wrote on last edited by
            #5

            @maxwell31 Hi,
            is dataList really an instance of QAbstractItemModel ?
            If yes, why are you wrapping your model with QVariant::fromValue() ?

            Better to do:

            ctxt->setContextProperty("myModel", dataList);
            

            Or share an object which share the model as property:

            class MyClass: public QObject
            {
                Q_OBJECT
                Q_PROPERTY(MyModel* model READ model)
            ...
            }   
            

            By this way you will have access to the interface of QAbstractItemModel (MyModel) from QML side (auto-completion)

            1 Reply Last reply
            4
            • M Offline
              M Offline
              maxwell31
              wrote on last edited by
              #6

              @Gojir4 said in Can I expose a C++ model as Q_Property?:

              is dataList really an instance of QAbstractItemModel ?
              If yes, why are you wrapping your model with QVariant::fromValue() ?

              Sorry, I just copied from the documentation. I corrected my post

              1 Reply Last reply
              1
              • M Offline
                M Offline
                maxwell31
                wrote on last edited by maxwell31
                #7

                Exposing the model like this

                class MyClass: public QObject
                {
                    Q_OBJECT
                    Q_PROPERTY(MyModel* model READ model)
                ...
                }
                

                works fine.

                1 Reply Last reply
                2
                • M Offline
                  M Offline
                  maxwell31
                  wrote on last edited by
                  #8

                  @Gojir4 said in Can I expose a C++ model as Q_Property?:

                  Better to do:
                  ctxt->setContextProperty("myModel", dataList);

                  would this also work if I have several instances?

                  1 Reply Last reply
                  0
                  • Pradeep P NP Offline
                    Pradeep P NP Offline
                    Pradeep P N
                    wrote on last edited by
                    #9

                    Hi @maxwell31

                    You can also try with qRegisterMetaType.

                    Q_PROPERTY(MyModel* model MEMBER m_Model NOTIFY modelChanged)
                    

                    then

                    qRegisterMetaType<MyModel *>();
                    

                    All the best.

                    Pradeep Nimbalkar.
                    Upvote the answer(s) that helped you to solve the issue...
                    Keep code clean.

                    1 Reply Last reply
                    4

                    • Login

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