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

前提是你 提交于 2019-12-24 07:58:13

问题


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?


回答1:


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 #




回答2:


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.



来源:https://stackoverflow.com/questions/46290088/in-a-pyside2-app-how-can-i-get-the-id-for-a-qwindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!