Important: Please read the Qt Code of Conduct -

QML and Qt handling Qobjects, QQuickItem

  • I am trying to make a game in qt and qml. I am including the most important part of the game. I have reduced it to simple form.

    Untitled Diagram.png

    n this game (above picture) there are 4 Boxes and two players (player1 and player2).

    1. In the first scenario player1 and 2 are both at rest place.

    2. In the second scenario player 1 moves to box 2 (assuming he gets 2 on dice ( generating random numbers) , this part is handled seperately and function will recieve the generated number only).

    3. In third scenario player 2 also gets 2 (assumption) so then player 2 moves to box 2 and player1 which was at box2 moves to rest place. ( all this is similar to Ludo, Snake ladder etc. games)

    The aim of this game is to see which player moves to box4 or beyond first wins the game.

    I can easily draw Box 1 to 4 in Qml and player 1 and 2. I can change the properties of these boxes in qt (c++) (using objectname from qml) via

    QQmlApplicationEngine engine;
    QObject *item = engine.rootObjects().first();
    auto child = item->findChild<QQuickItem*>("board");
    auto player1 = item->findChild<QQuickItem*>("player1");
    auto list = child->childItems();
    for (auto childItem: list )
        if (childItem->objectName() == "Box2")
            QTimer::singleShot(2000, [player1,childItem,child]{

    But Issue with this approach is that it is too lengthy and every time i have to assign x and y value. I was hoping if we have a better approach in qt to handle such things (useful in scaling the game).

    Basically a great approach would be if somehow player1 can have same object as box2. So that they appear at same place and when player 2 comes and box2 position then player 1 can be moved to rest position.

    But the problem is that I am unable to do or perform it.

Log in to reply