title: Python - How do I go about deleting a custom maya UI window when the user clicks outside of it?
body: <p>I have a custom window that basically acts as a popup whenever a user clicks on a shelf item.</p>
  
  <pre><code>def drawUI(): 
      mouse_pos = QtGui.QCursor().pos()
      print 'mouse x: %d y: %d' % (mouse_pos.x(), mouse_pos.y())
      if cmds.window("SET_SELECTION_UI", exists = True):
          cmds.deleteUI("SET_SELECTION_UI", window=True)
  
      cmds.window("SET_SELECTION_UI", title = "Example", wh = (102, 300), rtf=True, mnb = False, mxb = False, tb = False, sizeable = False, tlc=(mouse_pos.y(),mouse_pos.x()))
      cmds.columnLayout()
      cmds.button(label = "Set 00",   w = 100, command=button00)
      cmds.button(label = "Set 10",   w = 100, command=Button10)
      cmds.button(label = "Set 25",   w = 100, command=Button25)
      cmds.separator( height=10, style='double' )
      cmds.button(bgc=[1,1,1], label = "Cancel", w = 100, command=DeleteButton)
      cmds.showWindow() #shows window
  
      cmds.window("SET_SELECTION_UI", edit=True, tlc=(mouse_pos.y(),mouse_pos.x()))
  </code></pre>
  
  <p>The window appears properly, all the buttons do what they're supposed to do, and whenever a button is pressed, I delete the window. It's all fine and dandy.</p>
  
  <p>What I'm trying to do, is to detect when the user clicks anywhere outside of the window so that I can delete the UI (much like a pop-up menu works).</p>
  
  <p>Thanks!</p>
  
  <p><strong>ANSWER</strong></p>
  
  <p>I've integrated Cronicryo's script and suggestions into my own and it works perfectly now! Here it is:</p>
  
  <pre><code>from PySide import QtGui, QtCore
  import maya.OpenMayaUI as omui
  import pymel.core as pm
  from shiboken import wrapInstance
  
  fltr    = None
  qtObj   = None
  _WINDOW_NAME_ = "SET_SELECTION_UI"
  
  class FilterObj(QtCore.QObject):
      def eventFilter(self, obj , event):
          print event.type()
          if event.type() == event.WindowDeactivate:
              print "focused out"
              pm.window(_WINDOW_NAME_,edit=True,visible=False)
              return True #must return a bool set False to allow normal events to pass through or True to stop any further events
          else:
              return False
  
  def run(): 
      global fltr
      global qtObj
  
      if qtObj:
          del qtObj
  
      mouse_pos = QtGui.QCursor().pos()
      print 'mouse x: %d y: %d' % (mouse_pos.x(), mouse_pos.y())
      if pm.window(_WINDOW_NAME_, exists = True):
          pm.deleteUI(_WINDOW_NAME_, window=True)
  
      win = pm.window(_WINDOW_NAME_, title = "Set Select", mnb = False, mxb = False, tb = False, sizeable = False)
      pm.columnLayout()
      pm.button(label = "Do nothing",   w = 100)
      pm.separator( h=15, w=100, style='doubleDash' )
      pm.button(bgc=[1,1,1], label = "Cancel", w = 100, command=DeleteButton)
      pm.showWindow(win) #shows window
  
      pm.window(_WINDOW_NAME_, edit=True, wh = (102, 245),tlc=(mouse_pos.y(),mouse_pos.x()))
  
      qtObj = wrapInstance(long(omui.MQtUtil.findControl(_WINDOW_NAME_)), QtGui.QWidget)
      fltr = FilterObj()
      qtObj.installEventFilter(fltr)
  
  def DeleteButton(*args):
      pm.deleteUI(_WINDOW_NAME_)
  </code></pre>