Generic way to restore / set widgets to initial state ?
-
Is there a generic signal or function to set a widget to its initial state? I have a form that when loaded for the first time the state of the widgets have defaults, after interacting with the widgets the defaults are no more.
I have a QListWidget that I can use to select a context that effects the content of other widgets, what I really want to do is when the itemSelectionChanged signal occurs I would like to ensure all the widgets are in a known state.
-
@JonB , @J-Hilk , the dialog isn't being closed so the controls won't be deleted or recreated, the change in the QListWidget causes the controls to be updated live.
Having given this some thought I'm going to implement a signal, something like dataUpdate where the controls will connect to this signal and they're own slot will update the control.
-
@jsulm , I'm using property and setProperty to track the state of the widgets. I will investigate QSettings. I have reset signals that are emitted what the list selection changes, this sets the properties to {}. However the initial states of the widgets isn't being reset.
I think I'm going to have the same problem, presently I have a checkbox that initially isn't set, I change to a selection where the checkbox is set...this is ok, because the initial state was Unchecked then the new state changes to Checked, if I change the selection to another entry where the checkbox is also checked, that's where I get the problem because although the property is reset the widget isn't so when clicking the checkbox the new state is unchecked which because the property was reset becomes the new initial setting.
[Edit], I don't think QSettings is going to help, as it seems another way of doing exactly what I'm doing with property and setProperty, its the widgets I need to manage.
-
I believe there is nothing like an "initial state" based on your definition.
AQCheckBox
for example is always unchecked. Even when you set the tick in QtDesigner, the checkbox is unchecked but during moc in your compiled C-header, it sets the properties from yourui
-file, for example, and enables/checks theQCheckBox
.So you have to decide what you define as "initial state" and how/where do you save it and track it.
QSettings
could be an option... your reset signal could set the stored settings again ( -> "reset") -
@SPlatten
QSettings
isn't really the right tool for widget saving settings (well, IMHO, though if it suits you could, it's just one way of saving certain information in particular format, just like JSON). You cannot serialize, deserialize or "reset" (whatever that might mean) widgets.Really you just need to write explicit code. If by any chance you have a complicated widget state to reset, and you designed it in Designer,
QUiLoader
might suit you in certain specific circumstances. -
@JonB , @J-Hilk , the dialog isn't being closed so the controls won't be deleted or recreated, the change in the QListWidget causes the controls to be updated live.
Having given this some thought I'm going to implement a signal, something like dataUpdate where the controls will connect to this signal and they're own slot will update the control.
-
@SPlatten said in Generic way to restore / set widgets to initial state ?:
thought I'm going to implement a signal, something like dataUpdate where the controls will connect to this signal and they're own slot will update the control.
Just do it :)
You know best, how your widgets should look in your initial state or how to "reset" them. Can't be that hard to think about everything you want to undo / reset and then write a function which does so. Some function or slot which can be connected to your signal and which will clear, uncheck, flush, whatever... everything.
Btw: This behavior is not very uncommon. A dialog with some input fields and a button to "clear" / reset all input, while keeping the rest. On
QLineEdits
you can simply callclear()
(you probably know that)... for every other widget and maybe even custom ones, we can't help you, how your initial state might look like and how to get there :)