After creating a Qt window with PySide.you may want to assign a scriptJob window to it and remove the job after the window is closed with -p flag. For example in the following code:
import maya.cmds as cmds
import maya.OpenMayaUI as omui
import PySide.QtCore as QtCore
import PySide.QtGui as QtGui
from shiboken import wrapInstance
mayaPtr = omui.MQtUtil.mainWindow()
mayaWindow = wrapInstance(long(mayaPtr), QtGui.QWidget)
scriptJobWindow = QtGui.QMainWindow(parent = mayaWindow)
scriptJobWindow.setObjectName('scriptJobWindow')
scriptJobWindow.setWindowTitle('scriptJobWindow')
scriptJobWindow.show()
def scriptJob():
print "scriptJob callback"
jobNum = cmds.scriptJob(p="scriptJobWindow", e= ["SelectionChanged",scriptJob])
But you may be surprised to see that, even if the -p flag was specified, the scriptJob is still exists despite window being closed with the close button. How does that happen?
scriptJob -lj;
// Result: 0: "-permanent" "-event" "PostSceneRead" "generateUvTilePreviewsPostSceneReadCB"
.....
80: p='scriptJobWindow', e=['SelectionChanged', <function scriptJob at 0x000002E020AF3898>]
If you use Spy++ or similar tools, you'll find that the QWidget is still hanging out there.
Turns out that, when a QWidget is closed, it is simply hidden instead of being destroyed. In this case you may want to change its behavior, from just being hidden, to be destroyed. The solution is simple, add WA_DeleteOnClose attribute to QWidget.
import maya.cmds as cmds
import maya.OpenMayaUI as omui
import PySide.QtCore as QtCore
import PySide.QtGui as QtGui
from shiboken import wrapInstance
mayaPtr = omui.MQtUtil.mainWindow()
mayaWindow = wrapInstance(long(mayaPtr), QtGui.QWidget)
scriptJobWindow = QtGui.QMainWindow(parent = mayaWindow)
scriptJobWindow.setObjectName('scriptJobWindowWithDelete')
scriptJobWindow.setWindowTitle('scriptJobWindowWithDelete')
#Set WA_DeleteOnClose attribute
scriptJobWindow.setAttribute(QtCore.Qt.WA_DeleteOnClose)
scriptJobWindow.show()
def scriptJob():
print "scriptJob callback"
jobNum = cmds.scriptJob(p="scriptJobWindowWithDelete", e= ["SelectionChanged",scriptJob])
Now, when closed, the window is destroyed and the scriptJob is removed automatically.
Comments
You can follow this conversation by subscribing to the comment feed for this post.