This has been my workflow for creating UI’s dockable using a UI file.
Now I've done away with the whole loading the UI dynamically with QUiLoader() and here's why. I prefer to convert the UI file into a py file with pyside-uic and then load the UI class object into my UI code. This allows me to directly access all the UI objects in my code.
To do this I open up a cmd prompt and navigate to the folder that has the .ui file. Then I'll run for example:
pyside-uic myTool.ui -o myTool_ui.py
Then in my import declarations I'll import the UI class.
from myTool_ui import Ui_myTool
- My formatting may be off a bit so you’ll have to tweak it if you copy and paste.
For the dockable stuff you import this:
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
This would be my boiler-plate UI class:
from shiboken import wrapInstance
from PySide import QtGui, QtCore
import maya.OpenMayaUI as OpenMayaUI
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
def getMayaWindow():
"""
Get Maya window
:return: maya wnd
"""
mayaMainWindowPtr = OpenMayaUI.MQtUtil.mainWindow()
return wrapInstance(long(mayaMainWindowPtr), QtGui.QWidget)
class MayaDockUI(MayaQWidgetDockableMixin, QtGui.QDialog):
def __init__(self, parent=getMayaWindow(), *args, **kwargs):
super(MayaDockUI, self).__init__(parent=parent, *args, **kwargs)
self.setWindowFlags(QtCore.Qt.Tool)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
def dockCloseEventTriggered(self):
"""
Handle stuff when the dock is closed
"""
self.close()
self.deleteLater()
Then I'd import this class as well as my converted UI class into my tool class:
from mayaDockUI import MayaDockUI
from myTool_ui import Ui_myTool
class MySimpleWindow(MayaDockUI, UI_myTool):
def __init__(self, parent=None):
super(MySimpleWindow, self).__init__(parent=parent)
Then in my main() function I set the docking up:
def main():
global dialog
try:
dialog.dockCloseEventTriggered()
except:
pass
dialog = MySimpleWindow()
dialog.show()
dialog.setDockableParameters(dockable=True, floating=True, area='right', allowedArea='right')
# Make sure the docked UI pops up to the front
dialog.raise_()
Hope that’s clear and helps.
Cheers,
-Sean