Question

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:

  1. User changes toolbar visibility (hides it).
  2. Settings are saved with using the QSettings feature.

All other windows (can be as many as the user wants):

  1. Detect that Window 2 saved the settings.
  2. Apply the new settings.

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:

      • And so on...

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)?

Was it helpful?

Solution

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.

  1. 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.

  2. 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.

  3. Use a custom model, say derived from QAbstractListModel, and attach your windows to the dataChanged signal of the model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top