Congratulations to our 2022 Qt Champions!

Use QML runtime inside a game engine

  • Hi,
    I would like to create a GUI inside a game engine. I think using a QML based GUI would be best. How can I run a QML runtime synchronous with the rendered frames of my game engine? And how to render to an image?

    So inside the game engine I have a loop to render my frames. I would like to call the QML runtime here to render the next GUI frame into a image so that I can use this image as the texture for some control display inside the virtual world.
    Is that possible? How can I do that?

    And if my QML GUI is controlled by some C++ code, will that still work?

  • Hi Simmania,

    I have been looking at a similar problem (integrating Qt Quick with existing/custom 3D rendering code), and I found the two general approaches possible:
    Either you integrate your engine into qt, or or you integrate qt into your engine.

    For the first, you would basically render your 3D world "behind" the 3D UI, i.e. you have a bunch of custom render calls to render things and then Qt renders on top. There is a basic example here However, with this variant Qt will still control the overall render/event loop, which might not be what you want.

    Alternatively, you can use QQuickRenderControl to use your own render loop, which seems more along the lines of what you want. In this variant, you supply Qt with a framebuffer to draw to, and then QtQuick simply renders to this framebuffer which you may use in any way in your own 3D code. This is a little more complicated but there is another example to get you started:

    Both of these methods should still work with Qt 6.0.

    Hope this gets you started!

  • Thx a lot. I think the second option with the example is exactly what we need! But one thing is not clear to me. How can our game engine in this case communicate with the QML based GUI?

  • Using the loaded qml GUI, you can get access to the individual subobjects and modify the qml properties, and even attach C++ code to the signals. This here should get you started in that direction:

    Otherwise, you could also think about implementing your own C++-based qml components, where you have full control on how they behave on the C++ side. This of course only makes sense for some specific components and not for every button, only for things that probably need heavy customization from C++.

Log in to reply