Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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:
- # Dont delete child windows
- start()
- # Delete child windows when closed
- 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)
Advertisement
Add Comment
Please, Sign In to add comment