The scriptJob command has a ‘parent’ attribute that links the scriptJob lifetime to the UI lifetime, this is great when building UI that needs to e.g. monitor selection changes.
With the introduction of Qt based interfaces this gained some abmiguity, the parent needs to have a name, and the widget lifetime needs to be managed by maya. Here is a very minimal example of the steps required:
Give the Qt widget a name
I use uuid because name clashes are detrimental.
Actually delete the UI when the widget is closed.
This will actually delete the qt object and clean up nicely, I was surprised this wasn’t default behaviour.
Now when using a custom widget, of course it is possible to implement show & hide events to connect and suspend script jobs manaully, but this seems more integrated and allows hooking scriptjobs to parent dialogs instead of whichever custom widget inside.