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. Is it more efficient to go from qml to c++ or c++ to qml?
Forum Updated to NodeBB v4.3 + New Features

Is it more efficient to go from qml to c++ or c++ to qml?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 172 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.
  • J Offline
    J Offline
    Jay_emissary
    wrote last edited by
    #1

    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!

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

      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 and TreeView are based on this paradigm, but Repeater 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.

      1 Reply Last reply
      1
      • J Offline
        J Offline
        Jay_emissary
        wrote last edited by
        #3

        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!

        1 Reply Last reply
        0
        • J Jay_emissary has marked this topic as solved
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote last edited by
          #4

          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

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          J 1 Reply Last reply
          2
          • JKSHJ JKSH

            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

            J Offline
            J Offline
            Jay_emissary
            wrote last edited by
            #5

            @JKSH Agreed!

            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