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 dynamically pair qml objects with c++ objects
Forum Updated to NodeBB v4.3 + New Features

How to dynamically pair qml objects with c++ objects

Scheduled Pinned Locked Moved Solved QML and Qt Quick
29 Posts 5 Posters 584 Views 1 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.
  • jsulmJ jsulm

    @Jay_emissary said in How to dynamically pair qml objects with c++ objects:

    But instead, I'm getting a 0

    I'm not a QML expert, but maybe QML uses setters to set members like x?

    J Online
    J Online
    Jay_emissary
    wrote last edited by
    #20

    @jsulm As far as I know, yeah, you should be able to call a setProperty function in C++, and in QML you can directly set properties like x as well. Though, I'm currently trying to get x so I can verify that the c++ object is hooked up and can read QML's properties. It's important for me to get and set x and y properties so that I can perform functions such as smoothly moving objects across the screen at a framerate-dependent speed.

    jsulmJ 1 Reply Last reply
    0
    • J Jay_emissary

      @jsulm As far as I know, yeah, you should be able to call a setProperty function in C++, and in QML you can directly set properties like x as well. Though, I'm currently trying to get x so I can verify that the c++ object is hooked up and can read QML's properties. It's important for me to get and set x and y properties so that I can perform functions such as smoothly moving objects across the screen at a framerate-dependent speed.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote last edited by
      #21

      @Jay_emissary said in How to dynamically pair qml objects with c++ objects:

      I'm currently trying to get x so I can verify that the c++ object is hooked up and can read QML's properties

      But you wrote: "I added this statement to the HitObject's constructor". This will print 0, because when the parameter-less constructor is called 100 was not yet assigned to x.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      0
      • J Jay_emissary

        @Bob64 Oh, whoops! I must have overlooked that one. Thanks for catching that, lol... Now the QML objects are on the screen and the C++ objects get created as well!

        The only issue I have at this point is that I'm not so sure that the C++ object is associated with the QML objects. I added this statement to the HitObject's constructor:

            qDebug() <<property("x").toDouble();
        

        And because I set the x to 100 using in the following code, I should expect to receive that value when the c++ object is created.

        
            Repeater{
                model: hitObjectModel
                delegate:  Rectangle {
                        id: image      
                        width: 10
                        height: 40
                        color: "white"
                        x: 100
                        y:100}
            }
        

        But instead, I'm getting a 0. Could this possibly be because the QML is only reacting to the fact my object list has increased, and less so the fact that a C++ object has been created to pair with the corresponding QML object?

        B Offline
        B Offline
        Bob64
        wrote last edited by
        #22

        @Jay_emissary @Jay_emissary There is no implicit association of the C++ object with the QML object. Each object in your "view" is associated with a known index into the model and can access whatever "data" the model exposes at that index. In your case I believe you are exposing a whole object as your data, but often the underlying items in a model are more encapsulated and you just expose certain named fields at each index.

        Essentially there is a one way data flow from the model to the view. This doesn't mean that you can't expose functions from your model that can be used to update it. This would have to follow a strict process of updating the model items and providing appropriate signals to notify the view to update itself from the model.

        One simple thing that is worth asking: would it make sense for the X and Y positions to be define by properties of your model object?

        Otherwise, maybe it would help if you had another go at outlining what you are trying to achieve. For example, even if you don't know exactly how to write it, what is that you want to achieve in the QML layer and how will it relate to the C++ objects in the backend?

        J 1 Reply Last reply
        1
        • jsulmJ jsulm

          @Jay_emissary said in How to dynamically pair qml objects with c++ objects:

          I'm currently trying to get x so I can verify that the c++ object is hooked up and can read QML's properties

          But you wrote: "I added this statement to the HitObject's constructor". This will print 0, because when the parameter-less constructor is called 100 was not yet assigned to x.

          J Online
          J Online
          Jay_emissary
          wrote last edited by Jay_emissary
          #23

          @jsulm I see… I initially thought that the QML object gets created first, allowing me to then view the properties via C++. Apparently, the C++ object can be created and called upon a bit earlier. Thanks for pointing that out. I’ll keep that in mind.

          1 Reply Last reply
          0
          • B Bob64

            @Jay_emissary @Jay_emissary There is no implicit association of the C++ object with the QML object. Each object in your "view" is associated with a known index into the model and can access whatever "data" the model exposes at that index. In your case I believe you are exposing a whole object as your data, but often the underlying items in a model are more encapsulated and you just expose certain named fields at each index.

            Essentially there is a one way data flow from the model to the view. This doesn't mean that you can't expose functions from your model that can be used to update it. This would have to follow a strict process of updating the model items and providing appropriate signals to notify the view to update itself from the model.

            One simple thing that is worth asking: would it make sense for the X and Y positions to be define by properties of your model object?

            Otherwise, maybe it would help if you had another go at outlining what you are trying to achieve. For example, even if you don't know exactly how to write it, what is that you want to achieve in the QML layer and how will it relate to the C++ objects in the backend?

            J Online
            J Online
            Jay_emissary
            wrote last edited by
            #24

            @Bob64

            @Bob64 said in How to dynamically pair qml objects with c++ objects:

            There is no implicit association of the C++ object with the QML object. Each object in your "view" is associated with a known index into the model and can access whatever "data" the model exposes at that index. In your case I believe you are exposing a whole object as your data, but often the underlying items in a model are more encapsulated and you just expose certain named fields at each index.

            Ohhh gotcha. Given this information, I'll need to thoroughly think about how I can approach adding precise movement my objects while remaining within these guidelines

            @Bob64 said in How to dynamically pair qml objects with c++ objects:

            One simple thing that is worth asking: would it make sense for the X and Y positions to be define by properties of your model object?

            I don't think so, since each object needs to have its own independent x and y values.

            @Bob64 said in How to dynamically pair qml objects with c++ objects:

            Otherwise, maybe it would help if you had another go at outlining what you are trying to achieve. For example, even if you don't know exactly how to write it, what is that you want to achieve in the QML layer and how will it relate to the C++ objects in the backend?

            For some extra context, I'm creating a rhythm game in Qt. The HitObject/Notes are "falling" objects that the player "hits" to score points. Check https://www.youtube.com/watch?v=RSWoIsXfoM4
            at 1:12. The vertical scrolling arrows in the footage are essentially what I'm creating at the moment. Each arrow has their own X and Y coordinate and in video games you usually need a timestep/deltatime system to ensure that every object moves consistently across all platforms. This is why I emphasize accessing the deltaTimer object I've created in c++.

            But I think your explanation of the QML-> C++ workflow gives me a lot to consider, and honestly, probably answers the remaining questions I have about the process. I'm currently not in a place to hop on Qt just yet, but I'll continue to look into this later today. Oh, by the way, I know that I've been keeping this thread going for a while. Would you say it's best to just start a new one if I have another topic or one that closely resembles this one?

            Regardless, many thanks to you and everyone for having patience with me and providing more insight into the QML->C++ workflow. This is a topic that has had me puzzled for a while now, haha. Glad I have a better grasp on it now.

            1 Reply Last reply
            0
            • J Jay_emissary has marked this topic as solved
            • GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote last edited by
              #25

              I don't think so, since each object needs to have its own independent x and y values.

              Your "hit objects" position will be handled by your gameplay loop, these should be definitely be defined by your model. At least a "column" value and a "yProgress" value.

              J 1 Reply Last reply
              2
              • GrecKoG GrecKo

                I don't think so, since each object needs to have its own independent x and y values.

                Your "hit objects" position will be handled by your gameplay loop, these should be definitely be defined by your model. At least a "column" value and a "yProgress" value.

                J Online
                J Online
                Jay_emissary
                wrote last edited by
                #26

                @GrecKo I get the model determining a column value, but why yProgress? Shouldn't each element move independently of the other?

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  Bob64
                  wrote last edited by
                  #27

                  The way I understood it is that each element has its own entry in the model and this entry provides its own column index and yProgress. One option is that yProgress is a number between 0 and 1 that represents how far down the screen the object is. The delegate converts the column index and the progress number to an x-y coordinate that is meaningful in the geometry of the UI.

                  J 1 Reply Last reply
                  2
                  • B Bob64

                    The way I understood it is that each element has its own entry in the model and this entry provides its own column index and yProgress. One option is that yProgress is a number between 0 and 1 that represents how far down the screen the object is. The delegate converts the column index and the progress number to an x-y coordinate that is meaningful in the geometry of the UI.

                    J Online
                    J Online
                    Jay_emissary
                    wrote last edited by
                    #28

                    @Bob64 Understood. I can see how that works now. Thanks!

                    1 Reply Last reply
                    0
                    • GrecKoG Offline
                      GrecKoG Offline
                      GrecKo
                      Qt Champions 2018
                      wrote last edited by
                      #29

                      Yes that's exactly what I meant, thanks for explaining it Bob64.

                      1 Reply Last reply
                      1

                      • Login

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