UI in QML, logic in C++
-
Hi,
I would like to create an application composed of a tree of QML objects (a main window composed of UI controls,...).
I think it should be possible to define the UI appearence in QML code and all the logic in C++ code.
For example, in the QML code, I would like to define a StackView, set its properties (anchors, size,...), but the data of the StackView should come from C++ code. And C++ code shoulb be able to respond to signals emitted from the QML code.Ideally, each UI module would have a corresponding C++ class that would encapsulate all the logic of module.
However, have some difficulties to find how to do this in the documentation (it's so big that it's not easy to find exactly what I want).
Can you provide me with a pointer in the documentation that would explain me that? Or an example?
Thanks!
-
I think these two articles are a good start.
http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-exposecppattributes.html
http://qt-project.org/doc/qt-5.0/qtquick/qtquick-modelviewsdata-modelview.html#qml-data-models
-
Hey sorry for the delay for this answer.
And thanks for the links, I managed to make my C++ and QML code interact with each other.
The next step is to fill a StackView with data comming from the C++ side.
In fact, I would like to create a menu where each entry has sub-menus and so on. The list of entries and sub-entries must be build on the C++ side. Is the StackView the better base item to do that?At startup, the StackView should display item coming from a model from C++.
Then, when an item is clicked, a new list should be pushed to the stackview. This list must be filled from C++ code too.How could I do this? In fact I already managed to implement an "Item" class and a "ItemModel" (derived from QAbstractListModel.
Each Item contains a list of subItems (I think that's what we call a composite pattern).
Now, I don't know how to know which item has been clicked, and how to push its list of subitem to the stackview.Am I on the right path, or is there a better solution?
Are there some examples demonstrating this kind of use case?Thanks!
-
Have a look at this (lengthy) video on how to interact between the UI and the C++ logic.
https://www.youtube.com/watch?v=HN9mwHWH9DU
It for one recommends that you don't push data from C++ to the QML, but rather hvae the QML request the appropriate data from the C++ logic.
-
[quote author="t3685" date="1382981885"]Have a look at this (lengthy) video on how to interact between the UI and the C++ logic.
https://www.youtube.com/watch?v=HN9mwHWH9DU
It for one recommends that you don't push data from C++ to the QML, but rather hvae the QML request the appropriate data from the C++ logic.[/quote]
I thought it would be better if all the logic would come from C++ side...
Any way, I'll watch your video, it looks very interesting. Thanks! -
[quote author="JF002" date="1383686140"]
[quote author="t3685" date="1382981885"]Have a look at this (lengthy) video on how to interact between the UI and the C++ logic.https://www.youtube.com/watch?v=HN9mwHWH9DU
It for one recommends that you don't push data from C++ to the QML, but rather hvae the QML request the appropriate data from the C++ logic.[/quote]
I thought it would be better if all the logic would come from C++ side...
Any way, I'll watch your video, it looks very interesting. Thanks![/quote]
All the logic is in the C++, but it is the UI/QML that asks for data. Business/application logic in C++, UI logic in QML.
The data model should not know anything about how the data is displayed.