Calling setContextProperty() multiple times with same name
I'm working on an entity-component based application. Basically an entity is a list of components and a component is just an object which has some simple data (ints, strings, floats,...). I have a list which shows all the entities in my application and I have a custom qml component which i call the "component editor". The component editor is used to edit the simple data from a component of a selected entity(i use two way binding for this). The component editor can scroll through all the components of the selected entity (basically, there is a forward/back button which shows the next/previous component of the selected entity). I hope the basic idea is clear.
In my framework, I have a factory object, that for each component (each component has a type) generates a qmlInterfaceObject. The qml interface object is a qobject, which has the component as a member, it knows the qml file location for the component editor (the component editor has a loader which loads the qml file of the current component qml interface) and it has a string which identifies the context property which is used in the qml file to communicate with this qml interface (class variable).
When I select an entity in my list, the factory generates a qmlinterfaceobject (a pointer) for the first component of the entity. I do:
...->rootContext()->setContextProperty(qmlinterfaceobject ->getNameOfContextProperty(), qmlinterfaceobject);
Now when I select another entity, another qmlinterfaceobject is created. If it is generated for the same type of component as the current qmlInterfaceObject, I call setContextProperty with the same name, but another pointervalue. When this happens, i get some very strange results (basically the new component is updated with the values of the old ones which are still in the interface). Is it not allowed to setcontextproperty twice for a given name? Is it maybe in this case better to create a new context every time i switch entities? Or is there a better solution for this?
We are using setContextProperty sometimes and haven't seen any problems with it yet.
You could try to create some QObject "Root" that you use as the main entry into c++ (via setContextProperty) and give it a Q_PROPERTY like "currentQmlInterfaceObject" and update that to see whether this works differently for you.
@Moeren We use setContextProperty multiple times with the same name, the data is update to GUI OK but meet the problem with memory(The memory increase after call setContextProperty many times). Do you check it?