Is it more efficient to go from qml to c++ or c++ to qml?
-
Hello,
I'm dynamically spawning hit objects in c++, which consequently doesn't come with any on-screen visuals for the user to look at. To fix this issue, I'm planning on setting up some qml code to add visuals to each object.That said, I do have a couple questions about efficiency here:
Would it be better to connect each dynamically-produced c++ object to a qml file and control most of the functionality through the backend, or should I spawn a qml component for each dynamically-created c++ object and sync them up?
If the first is true, the syntax to get everything up and running is unclear to me. How can I let qt know to have a qml object ready for each c++ object? I took a look at the documentation, and apparently, there's a way to spawn a component from c++'s end.
I'm running this code in the object's constructor so the qml code will be set up on time and sync properly.
QQmlEngine *engine = qmlEngine(this); QQmlComponent component(engine, QUrl::fromLocalFile("HitObject.qml")); hitObjectQml = qobject_cast<QQuickItem*>(component.create()); hitObjectQml->setParentItem(hitObjectQml);
Could I safely set hitObjectQml's position properties through this method? I'm hoping to handle smooth and consistent movement from c++'s side via to deltatime handling. If it's actually more efficient to dynamically create qml components and hook them up to a c++ class, can I have some assistance in setting that up?
Thanks in advance!
-
Usually, programmatically creating QML from the C++ side is not recommended, but you might have some special considerations.
I don't know what a "hit object" is, but a common paradigm in QML is to have a backend C++ model and for your QML code to instantiate objects based on the model. Things like
ListView
andTreeView
are based on this paradigm, butRepeater
gives a more general ability to instantiate multiple objects from a model. I don't know if your use case would fit into this sort of paradigm. -
Ah, Okay. Since the hit objects are going to be used for a video game, I can best describe their behind-the-scenes function like enemies from any typical platformer: you can spawn multiple of the same type of object, the backend powers the movement, and the player can see and interact with the object on-screen as they play. I was worried that I might have been hooking up the backend in an unconventional way, and it looks like I was! Thank you for clearing that up for me. I'll be sure to look into the View and Repeater functions, and use QML to generate the objects instead, thanks!
-
-
The following post is probably very relevant to this discussion: https://forum.qt.io/topic/162421/how-to-dynamically-pair-qml-objects-with-c-objects
-
The following post is probably very relevant to this discussion: https://forum.qt.io/topic/162421/how-to-dynamically-pair-qml-objects-with-c-objects
@JKSH Agreed!