PDA

View Full Version : Create sharable container of multiple QWidgets



npatil15
4th April 2020, 10:10
Hello,

I have multiple QWidgets and want to share each widget with each other for sharing each other data.

I have created multiple widgets and there are other ways to share widget data by sharing pointer with each other, but is there an elegant solution where I can create a simple container to add all the widgets in it and make this container shareable to each other without passing to each other.

Please share your suggestions.

Thanks :)

d_stranz
4th April 2020, 16:08
What do you mean "share each other's data"? Do you want every widget to know about child widgets or other member variable values inside the other widgets? That is a bad idea.

If you want to be able to notify other widgets when one widget's state changes, then a better way is to use signals and slots.

It sounds like you might need some kind of "broadcast" system - when one widget changes, then all other widgets need to know about the change. This requires a "middle man" class that receives signals from each widget and then emits is own signal to the others. In this way you don't have to connect all of the widget signals and slots together - the middle man takes care of it.

You might be able to use QSignalMapper for this. With this class, every widget's signal is connected to the QSignalMapper::map() slot. In addition, you connect the signal mapper's mapped() signal to the slot each widget uses to listen for changes. There is a version of the QSignalMapper::map() and QSignalMapper::mapped() combo that allows the widget pointers to be passed.

If QSignalMapper itsef is not suitable (for example, you need to pass parameters - QSignalMapper works only for signals with no parameters), then you can implement your own QObject-based class that does the same idea - every widget's signal is connected to a slot in the middleman class, and every widget has a slot that is connected to a middleman signal. This could be very flexible - your middleman could have many signal / slot pairs, each one handling a different connection between the widgets.