Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to create Container for multiple QWidget?
npatil15 last edited by npatil15
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.
I would use a model to hold the data and use qdatawidgetmapper to let the widget read and write data.
If you share each widget to each widget, you get a very tightly coupled design with too many details
shared among the widgets.
Using a model, they do not need to know about other widgets at all.
npatil15 last edited by
Thanks for your quick reply,
I was looking over qdatawidgetmapper thoroughly.
One thing is not understanding about sharing data amoung the widget.
Example: Let say I have two widget : widget1 and widget2.
QDataWidgetMapper *mapper = new QDataWidgetMapper(this); QueryModel *model = new QueryModel(); // Have created custom model, which I want to share among the two widgets mapper->setModel(model); mapper->addMapping(widget1, 0); mapper->addMapping(widget2, 1); mapper->toFirst();
Now in widget1 I want to do some changes in the model, same in widget2 as well. So how I can share this model to both the widgets.
I have read about delegates but still not understand its working properly. I'm not sure how it will help to share model to both widgets.
Please give me some suggestions or examples to explain this.
you don't need to share the model to them. the QDataWidgetMapper handles this.
If you map them to same section of the model , they show same data
place 2 LineEdits on a form.
QDataWidgetMapper *mapper = new QDataWidgetMapper(this); QStringListModel *model = new QStringListModel(); QStringList list; list << "a" << "b" << "c"; model->setStringList(list); mapper->setModel(model); mapper->addMapping(ui->lineEdit, 0); mapper->addMapping(ui->lineEdit_2, 0); mapper->toFirst();
Then edit one of the LineEdits and press enter. as you can see the other will show the updated information
ps. Delegates are for custom editors and drawing. It has very little to do with the data.
Models hold the data
The view shows the data (the widgets)
Delegates allow custom editors and drawing in views. ( Not all Widgets can use delegates. List/Table/tree views can)
npatil15 last edited by
Yes, it works, but If I understand correctly, it will work if both widgets have same types of datatypes/inputs, isn't it?
Where I have two custom widgets, which have multiple methods and inputs to process this data and need to update in the same model. So how this will work?
well QDataWidgetMapper must be able to map the QVariant to
the widget so it only works with Widgets it understands.
If we really want the widgets to also do data processing behind the scene and not just editing
then you need to give it access to the model directly if QDataWidgetMapper editing is not enough.