Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot add model to QML

Cannot add model to QML

Scheduled Pinned Locked Moved Solved General and Desktop
qmlqquickwidgetc++model
11 Posts 2 Posters 3.1k 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.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by
    #2

    Your MainWindow is creating another QML engine which does not have "DataModel" set.

    (Z(:^

    M 1 Reply Last reply
    0
    • sierdzioS sierdzio

      Your MainWindow is creating another QML engine which does not have "DataModel" set.

      M Offline
      M Offline
      milan
      wrote on last edited by
      #3

      @sierdzio . So, how can i fix it. Do I add DataModel in mainwindow.cpp?

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

        @milan said in Cannot add model to QML:

        QQmlApplicationEngine* engine = new QQmlApplicationEngine();

        remove the engine since you're not using it anywhere. Or remove MainWindow if you are not using that. I don't know which is true in your case.

        Extract the engine from your ui->quickWidget and add the DataModel there. If you are using QQuickWidget, you can use https://doc.qt.io/qt-5/qquickwidget.html#engine.

        (Z(:^

        M 1 Reply Last reply
        3
        • sierdzioS sierdzio

          @milan said in Cannot add model to QML:

          QQmlApplicationEngine* engine = new QQmlApplicationEngine();

          remove the engine since you're not using it anywhere. Or remove MainWindow if you are not using that. I don't know which is true in your case.

          Extract the engine from your ui->quickWidget and add the DataModel there. If you are using QQuickWidget, you can use https://doc.qt.io/qt-5/qquickwidget.html#engine.

          M Offline
          M Offline
          milan
          wrote on last edited by
          #5

          @sierdzio .Thanks for your answer, it works. The initial value of gauge is set to 10. But the timer I have in my datamodel does not update the gauge value.

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

            @milan said in Cannot add model to QML:

            qDebug() << "Setting value:" << num;

            Do you see this message printed?

            (Z(:^

            M 1 Reply Last reply
            0
            • sierdzioS sierdzio

              @milan said in Cannot add model to QML:

              qDebug() << "Setting value:" << num;

              Do you see this message printed?

              M Offline
              M Offline
              milan
              wrote on last edited by
              #7

              @sierdzio . I see that DataModel object gets destructed after it is constructed.

              sierdzioS 1 Reply Last reply
              0
              • M milan

                @sierdzio . I see that DataModel object gets destructed after it is constructed.

                sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #8

                @milan said in Cannot add model to QML:

                @sierdzio . I see that DataModel object gets destructed after it is constructed.

                So make sure it lives long :-)

                (Z(:^

                M 1 Reply Last reply
                0
                • sierdzioS sierdzio

                  @milan said in Cannot add model to QML:

                  @sierdzio . I see that DataModel object gets destructed after it is constructed.

                  So make sure it lives long :-)

                  M Offline
                  M Offline
                  milan
                  wrote on last edited by milan
                  #9

                  @sierdzio. Yes I did that. Is qmlregistertype better way that setContextProperty I am using now? The C++ model should update the qml. Is it possible to use qmlregistertype in this context?

                  sierdzioS 1 Reply Last reply
                  0
                  • M milan

                    @sierdzio. Yes I did that. Is qmlregistertype better way that setContextProperty I am using now? The C++ model should update the qml. Is it possible to use qmlregistertype in this context?

                    sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #10

                    @milan said in Cannot add model to QML:

                    @sierdzio. Yes I did that. Is qmlregistertype better way that setContextProperty I am using now? The C++ model should update the qml

                    No it is not better, it server a different purpose. It makes the type recognizable (and creatable) in QML. Since you want to use a global data model (I think), then setting the context property is a better way for your use case. Just make sure DataModel object is not destroyed while your UI is alive. You can do it, for example, by declaring it as a member variable of MainWindow class, or by creating an object on heap and assigning MainWindow as parent.

                    (Z(:^

                    M 1 Reply Last reply
                    2
                    • sierdzioS sierdzio

                      @milan said in Cannot add model to QML:

                      @sierdzio. Yes I did that. Is qmlregistertype better way that setContextProperty I am using now? The C++ model should update the qml

                      No it is not better, it server a different purpose. It makes the type recognizable (and creatable) in QML. Since you want to use a global data model (I think), then setting the context property is a better way for your use case. Just make sure DataModel object is not destroyed while your UI is alive. You can do it, for example, by declaring it as a member variable of MainWindow class, or by creating an object on heap and assigning MainWindow as parent.

                      M Offline
                      M Offline
                      milan
                      wrote on last edited by
                      #11

                      @sierdzio. Thanks a lot for your answer.

                      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