Search code examples
pythonqt5mayapyside2

In a PySide2 app, how can I get the ID for a QWindow?


In the version of PySide2 that ships with Maya2017, the winId method on the QWindow class seems to be missing:

w.winId()
Error: AttributeError: file <maya console> line 1: 'PySide2.QtGui.QWindow' object has no attribute 'winId' # 

Is there a way to get this value from an existing instance of QWindow?


Solution

  • I used Maya 2018 for macOS 10.11.6. Try this code. It works.

    from maya import OpenMayaUI as omui 
    
    try:
      from PySide2.QtCore import * 
      from PySide2.QtGui import * 
      from PySide2.QtWidgets import *
      from PySide2 import __version__
      from shiboken2 import wrapInstance 
    except ImportError:
      from PySide.QtCore import * 
      from PySide.QtGui import * 
      from PySide import __version__
      from shiboken import wrapInstance 
    
    mayaMainWindowPtr = omui.MQtUtil.mainWindow() 
    mayaMainWindow= wrapInstance(long(mayaMainWindowPtr), QWidget) 
    
    w = QLabel("Hello, Window", parent=mayaMainWindow) 
    w.setObjectName('Label1') 
    w.setWindowFlags(Qt.Window)
    w.show() 
    

    And after typing:

    w.winId()
    

    you'll get something like this:

    # Result: 140640756092816 #
    

    enter image description here