PDA

View Full Version : Keeping track of an unknown number of widgets



Thibaut
14th May 2020, 14:30
Hi!

From my Maindow, I want the user to be able to open a setting window which will display x widget (check boxes).
In short, the user will load a data file, with each type of data being associated with a code. And in this setting window, he will be able to choose which data to display or not by checking or unchecking the check boxes. Thing is, I cannot determine how much data types there will be before loading the file.

I did try different methods and succeeded in creating an unknown number of widget when the window open.
But I didn't find a way to keep track of them to know which one are checked or not. Or rather, I didn't find a way to differentiate all of them an determine which one has its state changed.
Do you know a method for that?

d_stranz
14th May 2020, 17:20
It is not clear to me exactly what you are trying to do. It sounds like you are confusing the user action of selecting which widgets to create with the action of actually creating them. To select the widgets to create, all you need is a dialog with a list widget with check boxes. When the dialog is closed, you examine the entries in the list to see which ones to create.

If these widgets will be created by the MainWindow (or some other widget) in your application, then your MainWindow needs a data structure (QVector, QList, QMap, etc.) which can store the pointers to these new widgets as they are created. If the user can close these widgets at any time, then your MainWindow class needs a slot that can be connected to each widget's destroyed() signal so the MainWindow can remove the pointer from the data structure.

If each of these widgets is displaying some data that is held in your program, then you need a general mechanism to tell the widgets when the data's state has changed. In my applications, I usually have a "document" class that holds all of the program data. When this data is modified, the document class emits one signal (documentChanged(), say) with a flags argument that tells which data property(ies) were changed. Each display widget is connected to this signal when it is created, and in its slot to handle the signal will look at the flags argument to see if it is interested in that change. If it is, it updates its display.

If this doesn't help, maybe you can give some more detail about what you are trying to do.

Thibaut
15th May 2020, 08:05
You understood well.
I did as you said and put my checkbox in a Vector as I create them, and then send a signal to recover their individual state. It seems to work well! I will do some more tests to see
I was also thinking of doing a "document" class as you said, maybe it would be easier with that.
Thanks!