Nominate our 2022 Qt Champions!

Project "componentization"

  • Hello There,
    I have the following problem: I was developing a QML based application, it contained QML frontend and a lot of logic behind in the backend (c++ classic Qt). The communication between QML and C++ was done using QQmlContext (setContextProperty). The backend and frontend are quite in tact.

    Now I need to create a pure component out of the QML frontend part. Even though I have already extracted everything in a way, that I can create a new component in code, that contains all the features, there are still the Qt/C++ connections, so the components has to come with the c++ overhead. This doesn't feel right and at this point I don't know how to handle multiple instances of the component.

    My question is: are there any guidelines on creating complex components in QML? Specifically, in a way where there will be multiple instances of that component, while each of it, has to have an unique connection for data flow (multiple structures and arrays) with the c++ part.

    I know that this is a very generic question, I don't expect any specific answers. I think I am looking for some QML+Qt design pattern.

    I would appreciate all help.

  • One idea is having a "generator" C++ object that spawns an instance of the appropriate object(s) when the component is created. However, after the object is created I don't know how to track the lifetime of the object inside the component. The lifetime would have to be associated with the component somehow. I know you have to be careful with object lifetime and qml accessing objects. qml likes to delete objects it thinks it owns.

    Another idea is to have a list of objects and the current object being used for a particular component would be an tracked by an index set in the component. Again there would be the lifetime issue. On a list you could have the component destroy the object by calling a method on the list however.

  • Maybe this is the way to do this:
    It looks like you can have multiple contexts defined. Each component can have its own context.

  • @fcarney Hello, thank you for answer. I Like your idea. I have also found a blog post with similar concept:

Log in to reply