简体   繁体   中英

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?

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 #

在此处输入图片说明

Andy's example works for me in both Maya2018 and the latest release of Maya2017, but throws an exception in at least the initial release of Maya 2017.

I expect the problem was caused by a bug in PySide2 that got fixed along the way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM