I am writing a SDI (Single Document Interface) application with the Qt framework.
I am currently stuck with a "small" problem: If I change a preference in one MainWindow, I would like that all the other instances of the same MainWindow to load that preference, say:
Window 2:
All other windows (can be as many as the user wants):
I have tried many things, such as using SIGNALS/SLOTS, but I cannot change all windows because the way that MainWindows are created in my app are so:
Window 1 creates Window 2:
Window 2 creates Window 3:
Window 3 creates Window 4:
This means that if a change a value in Window 3, only Window 2 will load the new settings.
Do you know a way of doing this (how pass values to all children (and parents) of a window)?
You can use QApplication::topLevelWidgets()
to get a list of top-level widgets (windows). You'd call it like this: qApp->topLevelWidgets()
.
There are multiple ways of notifying those widgets about the changes. I've listed some of them below; the list is by no means exhaustive, there are many more ways of doing it.
You can use a QObject
to store your settings in dynamic properties. You can emit a signal when any property is changed. Reimplement QObject::event()
to handle QDynamicPropertyChangeEvent
that the object will receive whenever a dynamic property is changed. Whenever a property is changed, you can simply store it in QSettings. The object's initial property values can be retrieved from QSettings upon construction.
Emit a signal manually whenever you change settings, and connect it to each SDI window at the point when you create it. This doesn't require using the topLevelWidgets()
method.
Use a custom model, say derived from QAbstractListModel
, and attach your windows to the dataChanged
signal of the model.