Access shared data in childs
-
Hi to all,
I'm relatively new to the Qt/C++ world and as my first project I'm creating a timeline editor widget that I could use after in a bigger project.
I'm facing some design issue and I would ask an hand understand which solution could I use or is usually used in case like that.
In my project I have a main widget with many sub widgets at different level of depth. I have some widget that is 5 level under the main widget.
Those widget use some common data to create itself, like in my case a list/vector of track objects, so I need to have those data and some common data (like some styling data) in many of my widget.Coming from other programming language/framework, recently the new web frontend framework like vuejs/angular, my first idea is to use some of the principle of the reactive programming and have this data the main widget that will act like a storage (or will have and object that will act like a storage) and pass the data to all the childs tree that need it. When a child do an action, it have to call a method that will mutate the central storage (the main widget storage).
I have some barely idea of how to implement that but first, does something like that, that extensively use an event system, make sense in Qt?
What other methods/pattern is usually used to have a single data in many sub widgets, and call the update of them when a data is changed?In case the reactive programming path is usable, what's the best way to pass the data from and to the central storage and the various children?
Passing a reference of the central storage down to the childrens, call some mutator function on it and attach to some event emitted from it to know when the data is updated? Passing that storage object with a sort of dependency injection system?
Or passing the needed data from parent to child (like the component system of vue/react/etc) and emit event to mutate the data from child to the parent until the main widget?I've searched some info in the forum but I haven't found a complete answer that compare the pros/cons of the various solution.
Thanks to all for the answers
Cheers
Mix -
@mix359 said in Access shared data in childs:
Those widget use some common data to create itself
An object does not create itself, this is done by another object. So, the question is: who creates this widget? The one who creates it can provide the data (if it has it).
-
@jsulm thanks for the answer.
Yes the widget will be created by another widget outside of this project, and that widget will pass some data to the main widget in this project.My questions was about handling those data that my main widget have in the sub widgets (use them and update them from a deep child widget)
Thanks again
Mix -
Hi,
This highly sounds like a use case for the model view paradigm.
Have a clear separation between the UI and the data storage. Then share that data storage where needed. Using Qt model classes you can then propagate the modification pretty easily.
-
@SGaist thanks for the suggestion, I've discovered a big part of Qt that I was missing...
I'm studying the model/view in Qt and it solve part of my problem: I can use a model for the tracks that are my main data.
But even with this method I'm still missing how to pass/transport some other data that my views need, some attribute or settings that I can pass to my main widget and will be used in his (deep) childs.
For example, let say I have a preference/attribute that change the grid size of my timeline. This attribute is passed in some way to my main widget, and it's used on a sub child to render the grid.
When I change that attribute, the sub child should receive an event that make it read the data again and re render/update.In a framework like vue those are props that I set in the main component and is passed down as a props to the childrens. Those props are reactive so when you update them, all the function that use them are called again to be updated.
There's something similar in Qt? Some pattern I could use in this case?
Does it make sense to use two models? One for the main data, and another for the options/preferences?
Or I need to create my own system based on a QObject that is shared by the widgets, and use the signal/slot method to propagate the changes to his data?
Thanks
Cheers Mix -
You can have as many models as you want.
Only interested childs should access these settings.
-
If you are thinking about a QAbstractItemView, they usually handle only one model. But you can put a proxy that merges several models into one.
However, that's not what I'm suggesting here, you don't need to have a view to use a model. You can very well use whatever pleases you. Just connect the appropriate signals you want to handle to keep your objects up to date.