Skip to content

Instantly share code, notes, and snippets.

@justinfx
Last active December 19, 2015 14:39
Show Gist options
  • Save justinfx/5970780 to your computer and use it in GitHub Desktop.
Save justinfx/5970780 to your computer and use it in GitHub Desktop.
Showing a test, inside and outside of Maya, of child windows still remaining alive after being closed. Test can toggle the WA_DeleteOnClose attribute on each child widget:
#!/usr/bin/env python
"""
Showing a test, inside and outside of Maya,
of child windows still remaining alive after being closed.
Test can toggle the WA_DeleteOnClose attribute on each child widget:
# Command line (dont delete)
$ ./child_window_test.py
# Command line (delete)
$ ./child_window_test.py -delete
# Inside Maya
import child_window_test
child_window_test.start(delete=True)
"""
from PySide import QtCore
from PySide import QtGui
IS_MAYA = QtGui.qApp is not None
if IS_MAYA:
import shiboken
import maya.OpenMayaUI as mui
def getMayaWindow():
ptr = mui.MQtUtil.mainWindow()
return shiboken.wrapInstance(long(ptr), QtGui.QWidget)
def checkChildren(parent, timer):
count = len([c for c in parent.children() if isinstance(c, TestWindow)])
print "Num. of TestWindows:", count
if not count:
timer.stop()
class TestWindow(QtGui.QMainWindow):
def __init__ (self, parent=None) :
QtGui.QMainWindow.__init__(self, parent=parent)
self.setObjectName("testWindow")
self.layout = QtGui.QHBoxLayout()
self.label = QtGui.QLabel("test panel")
self.layout.addWidget(self.label)
self.setLayout(self.layout)
def start(delete=False):
# Create a main window if we are running outside Maya
if IS_MAYA:
mainWindow = getMayaWindow()
else:
app = QtGui.QApplication([])
mainWindow = QtGui.QMainWindow()
mainWindow.setWindowTitle("I am the Maya Main Window")
mainWindow.resize(600,400)
mainWindow.show()
mainWindow.raise_()
# Make some child windows
for i in xrange(5):
window = TestWindow(parent=mainWindow)
window.setAttribute(QtCore.Qt.WA_DeleteOnClose, delete)
window.setWindowTitle("I am a test window")
window.resize(300,200)
window.show()
window.raise_()
# Check the children every 1.5 seconds
timer = QtCore.QTimer(mainWindow)
timer.timeout.connect(lambda: checkChildren(mainWindow, timer))
timer.start(1500)
# Stop the check timer after 15 seconds
QtCore.QTimer.singleShot(15 * 1000, timer.stop)
if not IS_MAYA:
app.exec_()
if __name__ == "__main__":
import sys
args = sys.argv[1:]
delete = '-delete' in args
start(delete)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment