PyQt and PySide Widget Best Practices
Maintain a Reference to your Widget
If you are using PyQt or PySide to customize Maya’s user interface, you should make sure to parent your widget under an existing Maya widget, such as Maya’s main window. Otherwise, if the widget is un-parented, it may be destroyed by the Python interpreter’s garbage collector if a reference to it is not maintained.
The following code sample exemplifies this best practice. Note that this code will also work by importing the PyQt module instead of PySide.
If you are using the PySide mix-in classes described below, this parenting will be handled for you automatically.
Use the maya.app.general.mayaMixin Classes
The maya.app.general.mayaMixin module contains classes to help simplify the integration of PySide-based widgets into Maya’s UI.
The MayaQWidgetBaseMixin class handles common actions for Maya Qt widgets during initialization such as: auto-naming a widget so that it can be looked up as a string through maya.OpenMayaUI.MQtUtil.findControl(), and parenting the widget under the main Maya window if no parent is explicitly specified, so that the window does not disappear when the instance variable goes out of scope (see Maintain a Reference to your Widget above).
To use the class, ensure that it appears in your widget’s list of parent classes before the Qt class that your widget derives from.
For information on the methods and properties provided by these classes, use the Python’s help function in the Python tab of the Maya Script Editor (for example, help(MayaQWidgetDockableMixin)).
The MayaQWidgetDockableMixin class provides support for Maya’s dockable actions. The docking behavior of the widget is controlled through parameters passed to its show() method, such as: dockable to specify whether the widget should be dockable, area to specify its default dock area, and so forth.
To use the class, ensure that it appears in your widget’s list of parent classes before the Qt class that your widget derives from.