Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

Exposing custom type class instance to QML code

$
0
0
Dear all, I am about to develop a small labyrinth game with Qt 5.1.1. I would like to implement the whole game logic within C++ and expose the internal state of the “game engine” to QML for visualization. Game Logic and GUI code should talk to each other by using signals and slots. The game is based on a labyrinth class. Each level of the game is a subclass of this labyrinth and holds the layout and the items within the labyrinth. Now I would like to set a new level in my application logic and populate the corresponding level data to QML for drawing the whole labyrinth. Therefore, I have a class “Level” which I registered with the Qt meta type sytem using qmlRegisterType() and qRegisterMetaType(). My game logic emit’s a signal signal sigLevelChanged(const Level& newLevel) which should be connected to a function function slotLevelChanged(level) in my QML code. However, I am not able to connect the signal correctly. The compiler always complains that there is no corresponding slot slotLevelChanged(const Level& newLevel) within the QML code available. This is indeed the case as Javascript is an untyped language but how can I deal with this. Do I have to use QVariant in order to send my data to QML code. But then I would somehow need to cast this QVariant back to my “Level” type. Furthermore, I don’t know how I would be able to access the members of my “Level” class from within QML. Do I have to declare all member variables of the “Level” class with the Q_PROPERTY macro in order to be able to access them? Last but not least: Is this the preferred way to do what I want to achieve? I know it might be easier to solve this with Javascript but as I am not sure whether to complete the GUI part with QML or QtWidgets I would like to stick to C++ for implementing the game logic. Thanks for any comment on this and with best regards, Markus PS.: I was googeling a lot on this topic and came across the official docs and different websites dealing with this topic. However, I was not able to find a solution for my particular problem.

Viewing all articles
Browse latest Browse all 4972

Trending Articles