I would like to know if you think it is reasonable to use a pattern similar to RAII to manage GUI state in Qt. By GUI state I mean the following: I’ve some widgets (including the mouse cursor state) I want to go (not) visible/enabled/changed after I leave some method, and I don’t want to put everything I do in a giant try catch in this way:
widget1->show();
...
widgetN->show();
try {
...
}
catch(...) {
widget1->hide();
...
widgetN->hide();
throw;
}
widget1->hide();
...
widgetN->hide();
If I create an object that allows me to associate the hide/setEnabled/setCursor function (maybe a boost functor) on its constructor and that calls this associated function on its destructor (provided that all exceptions this function can throw are eaten/lost in the destructor) I can have a much cleaner code. Is this reasonable? what I’m not seeing?
Any comment/suggestion will be really welcomed.
Thanks in advance,
Federico
It’s completely reasonable. The technique you’re after is called ScopeGuard, called ScopeExit in Boost.
The idea is that you define some code you want to run at the end of the scope when you first make your changes, and the rest is handled. You can “dismiss” the code if desired.
I’d type an example but I’m on my phone.