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
Forum Updated to NodeBB v4.3 + New Features

Cannot add model to QML

Scheduled Pinned Locked Moved Solved General and Desktop
qmlqquickwidgetc++model
11 Posts 2 Posters 2.2k 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.
  • S Offline
    S Offline
    sierdzio
    Moderators
    wrote on 20 Sept 2018, 10:05 last edited by
    #2

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

    (Z(:^

    M 1 Reply Last reply 20 Sept 2018, 10:33
    0
    • S sierdzio
      20 Sept 2018, 10:05

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

      M Offline
      M Offline
      milan
      wrote on 20 Sept 2018, 10:33 last edited by
      #3

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sierdzio
        Moderators
        wrote on 20 Sept 2018, 10:39 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 20 Sept 2018, 11:00
        3
        • S sierdzio
          20 Sept 2018, 10:39

          @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 20 Sept 2018, 11:00 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
          • S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 20 Sept 2018, 11:21 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 20 Sept 2018, 11:22
            0
            • S sierdzio
              20 Sept 2018, 11:21

              @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 20 Sept 2018, 11:22 last edited by
              #7

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

              S 1 Reply Last reply 20 Sept 2018, 11:22
              0
              • M milan
                20 Sept 2018, 11:22

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

                S Offline
                S Offline
                sierdzio
                Moderators
                wrote on 20 Sept 2018, 11:22 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 20 Sept 2018, 11:24
                0
                • S sierdzio
                  20 Sept 2018, 11:22

                  @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 20 Sept 2018, 11:24 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?

                  S 1 Reply Last reply 20 Sept 2018, 11:27
                  0
                  • M milan
                    20 Sept 2018, 11:24

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

                    S Offline
                    S Offline
                    sierdzio
                    Moderators
                    wrote on 20 Sept 2018, 11:27 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 20 Sept 2018, 11:34
                    2
                    • S sierdzio
                      20 Sept 2018, 11:27

                      @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 20 Sept 2018, 11:34 last edited by
                      #11

                      @sierdzio. Thanks a lot for your answer.

                      1 Reply Last reply
                      0

                      11/11

                      20 Sept 2018, 11:34

                      • Login

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