Important: Please read the Qt Code of Conduct -

Making a custom QML type element or just use regular elements

  • I am making a game. There are two characters in the game like 'hero' and 'enemy'. I have made separate classes of them. Should I declare a new custom type for each character like

    import QtQuick 1.1
    import hero 1.0

    or just use a regular element like

    import QtQuick 1.1
    source:hero.getImgSource() //method declared in hero class

    So, I am in confusion to make a new element or just use the existing one and use the C++ bindings with QML. Is there any other approach to do so?

    Thanks in advance...

  • You can either use C++, Python or other bindings or you can implement game logic directly in QML with Javascript. It depends on your approach as it is your decision.
    However, it is good to separate logic (model) from display (view) in my opinion, making the application architecture more elastic and clear to others - and to yourself later on.

  • Hi,
    in V-Play we have made good experience with the component-based architecture and an entity system that we designed. What this means, is that you have components for displaying an entity (defined in the set of "visual components":, some for collision detection, sounds, and then there are some for logic like AI behaviors or others.

    Here is an example how one of a game entity in the open-source game "StackTheBox": looks like, taken from Wall.qml:
    import QtQuick 1.1
    import VPlay 1.0
    import Box2D 1.0 // for accessing the Body.Static type

    EntityBase {
    entityType: "wall"

    // this gets used by the top wall to detect when the game is over
    signal collidedWithBox

    // this allows setting the color property or the Rectangle from outside, to use another color for the top wall
    property alias color: rectangle.color

    Rectangle {
    id: rectangle
    color: "blue"
    anchors.fill: parent
    BoxCollider {
    anchors.fill: parent
    // use "import Box2D 1.0" to access the Body.Static type
    bodyType: Body.Static // the body shouldnt move

     fixture.onBeginContact: collidedWithBox()


    It does not really matter if you write the base components in QML or C++, although I would start with QML & JavaScript and only write the stuff that requires high performance in C++ because you are much faster to develop and iterate quicker.

    You can also have a look at our other open-source games, maybe you find some concepts that you can use in your game:

    Cheers, Chris

Log in to reply