PDA

View Full Version : Create dynamic QML components from within C++! How to?



jackmack
11th October 2012, 16:22
Hi all,

I'm using Qt over some years but without QML. Now I want to add the flicker functionality to my applications UI.
The application logic itself is coded in C++ (inherited sockets, database classes e.g.).

My applications UI should be fully configureable for diffrent customers, what means:

Customer 1: wants to have 5 sliding pages
Customer 2: wants to have 7 sliding pages

and so on. The number of pages should be loaded by a custom xml config file in the C++ part of application.

The first trips into QML examples were successfully. The example ..\4.8.2\examples\tutorials\gettingStarted\gsQml\p arts\part2 makes exactly what I want. Yeahh!

My question here:
How I can create/adding QML elements dynamically from C++ and insert it into the QML UI?

I'm confused about that statements in the QML documentation, chapter "Dynamic Object Management in QML":

"QML provides a number of ways to dynamically create and manage QML objects. The Loader, Repeater, ListView, GridView and PathView elements all support dynamic object management. Objects can also be created and managed from C++, and this is the preferred method for hybrid QML/C++ applications (see Using QML Bindings in C++ Applications)."

And then you can read this here in "Using QML Bindings in C++ Applications":

"Warning: While it is possible to use C++ to access and manipulate QML objects deep into the object tree, we recommend that you do not take this approach outside of application testing and prototyping. One strength of QML and C++ integration is the ability to implement the QML user interface separately from the C++ logic and dataset backend, and this strategy breaks if the C++ side reaches deep into the QML components to manipulate them directly."

What next?

The QML examples uses always a fixed, known, predefined QML UI.

Can anybody make a suggestion or help me?

Thanks

Urthas
20th October 2012, 11:47
Hm, well, presuming you have a SOURCE of dynamic data, you can use that data to populate a model, then expose that model to a QML view. See this tutorial (http://cdumez.blogspot.ca/2010/11/how-to-use-c-list-model-in-qml.html) for more...it's a bit dated, and QDeclarativeItem may not be your poison, but it is very helpful for wrapping your head around the basic, step-wise concepts.