Python qt: environments

From wikinotes

Qt is fantastic, and can be used on just about any platform. However it's usage varies a little when it is used to write a standalone program, or to script within a program.

Standalone

Example

#!/usr/bin/env python
from Qt import QtWidgets, QtCore
import sys

app = QtWidgets.QApplication(sys.argv)

# build widgets
win = QtWidgets.QWidget()
lyt = QtWidgets.QVBoxLayout()
btn = QtWidgets.QPushButton('Ok')

# widget positions
win.setLayout(lyt)
lyt.addWidget(btn)

# widget attrs
win.resize(600, 300)
win.setWindowTitle('Simple Window')

win.show()

sys.exit(app.exec_())

Autodesk Maya

Example

Maya already has a QApplication, and a QMainWindow.

#!/usr/bin/env python
from Qt import QtWidgets, QtCore
from maya import OpenMayaUI
import shiboken2


# build widgets
ptr = OpenMayaUI.MQtUtil.mainWindow()
mainwindow = shiboken2.wrapInstance(long(ptr), QtWidgets.QWidget)

win = QtWidgets.QWidget(parent=mainwindow)
lyt = QtWidgets.QVBoxLayout()
btn = QtWidgets.QPushButton('Ok')

# widget positions
win.setLayout(lyt)
lyt.addWidget(btn)

# widget attrs
win.resize(600, 300)
win.setWindowTitle('Simple Window')

win.show()

Mixin Classes

Autodesk provides a variety of mixin classes to help you integrate your windows into Maya. These are found under /usr/autodesk/maya2018/scripts/.... There are some quirks, which are documented here:

from maya.app.general import mayaMixin

class MainWindow(mayaMixin.MayaQWidgetDockableMixin, QtWidgets.QMainWindow):
    def dockCloseEventTriggered(self):
        pass

    def closeEvent(self, event):
        pass

The MayaQWidgetDockableMixin allows your widgets to be dockable within maya's mainwindow. It does not emit a closeEvent, when window.show(dockable=True). Instead you must subclass window.dockCloseEventTriggered().