Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Best approach to manage multiple instance model update ?
HubertDu47 last edited by HubertDu47
I have nearly the same problem as here: https://forum.qt.io/topic/108042/how-to-emit-signal-dynamically, but instead of having pointer, I have QWeakPointer.
I have an abstract class, say: "AModel", which have data to be updated. I create an instance for each "kind" of model (which is represented by an enum). I have a manager class "ManagerClass" which receives the data from a WebSocket and has to dispatch them to the right instance.
The data are only stored if the user needs them, therefore, I only instantiate the "AModel" if the user ask them and I give to him a QSharedPointer. I only keep a QWeakPointer in the "ManagerClass", in case the user asks a kind of "AModel" currently used (I transform the QWeakPointer to strong ref, test if it's not null, and return it to the user (or if its null, creates a new instance)).
The "ManagerClass" and the WebSocket are in a Thread "A" and the user uses a HMI in the main thread. (In the HMI thread the user can only get constant data and cannot modify class attributes)
I only see three solutions to this problem:
- Either every instance received all the messages and say: "this is not for me", but if I receive 1 message every 30ms, I will have 1 msg multiply by the instance number to manage. At first view, it doesn't seem really nice
- Or I write a private signal in the "ManagerClass" (or in a companion class) for each kind of instance and connect this signal to the right instance at its creation
- Or each time I received a message for an instance and I have a QWeakPointer, I transform it to a QSharedPointer (test if it's null) and call the method to update data. Seems to be the more elegant solution, but I don't know if there is one which can be better
I'm stuck here and I don't know what will be the better solution (or if one better exists). What do you think about it?