The tricky part here is to read those settings and make them available elsewhere – since once you destroy the dialog you can no longer read the values. That was the original reason I wrote the pyqtconfig module, to sync the data from config dialogs into a dictionary which you can then return as the “result” of a dialog.
You have a couple of options:
- connect signals on these, sending additional data (the value name) and receive this somewhere, to store it in a data object. The problem is you still need some way to set the initial state, and you can’t postpone the changes until the dialog is OK/Applied
- load/unload the data from somewhere when opening the dialog (this is what I’d recommend)
You can implement something similar to pyqtconfig by creating a map of name, getter, setter and then using this to set/reset the values, e.g.
data = {
'checkbox1': Qt.Checked,
'checkbox2': Qt.Unchecked,
'somestring': "hello",
'int': 23
}
options = {
'checkbox1': (obj.checkbox1.checkState, obj.checkbox1.setCheckState)
'checkbox2': (obj.checkbox2.checkState, obj.checkbox2.setCheckState)
'somestring': (obj.strinput.text, obj.strinput.setText)
'int': (obj.spinbox1.value, obj.spinbox1.setValue)
}
# Set values from dictionary
for k, (_, setter) in options.items():
value = data[k]
setter(v)
# Copy values into dictionary
for k, (getter, ) in options.items():
data[k] = getter()
In pyqtconfig the mapper getter/setters are handled automatically based on the type of the widget. For this you just need a table of object types and getter setter names, e.g.
data = {
'checkbox1': Qt.Checked,
'checkbox2': Qt.Unchecked,
'somestring': "hello",
'int': 23
}
mappers = {
'QCheckBox': ('checkState', 'setCheckState'),
'QLineEdit': ('text', 'setText'),
'QSpinBox': ('value', 'setValue'),
}
options = {
'checkbox1': obj.checkbox1,
'checkbox2': obj.checkbox2,
'somestring': obj.strinput,
'int': obj.spinbox
}
# Set values from dictionary
for k, widget in options.items():
widget_cls = widget.__class__
getter_name, setter_name = mappers[widget_cls]
value = data[k]
setter = getattr(widget, setter)
setter(v)
# Copy values into dictionary
for k, widget in options.items():
widget_cls = widget.__class__
getter_name, setter_name = mappers[widget_cls]
getter = getattr(widget, getter_name)
data[k] = getter()
You can wrap this up into some kind of handler object – I’d recommend a central “config” manager which you can pass a series of widgets and config names and it handles the rest.
fyi you could do the same thing using QSettings
and .setValue()
and .value()
in place of the dictionary data
gets/sets if you prefer. The types for all standard widgets should be handled automatically.