我在 Maya 中使用过 PyQt4 很多次,通常我发现切换到 PySide 很容易,但我无法获得指向主窗口的指针。也许有人可以理解出了什么问题。
这是我在 PyQt4 中所做的:
import sip, PyQt4.QtCore
import maya.OpenMayaUI as mui
ptr = mui.MQtUtil.mainWindow()
parent = sip.wrapinstance(long(ptr), PyQt4.QtCore.QObject)
这很好用。当我在 PySide 中尝试同样的操作时:
import sip, PySide.QtCore
import maya.OpenMayaUI as mui
ptr = mui.MQtUtil.mainWindow()
parent = sip.wrapinstance(long(ptr), PySide.QtCore.QObject)
我收到以下错误:
# Error: wrapinstance() argument 2 must be sip.wrappertype, not Shiboken.ObjectType
# Traceback (most recent call last):
# File "<maya console>", line 4, in <module>
# TypeError: wrapinstance() argument 2 must be sip.wrappertype, not Shiboken.ObjectType #
有人知道怎么回事吗?
最佳答案
您需要导入 shiboken
而不是 sip
并将 QWidget
传递给 wrapInstance
而不是 QObject
编辑:Maya2017 包含shiboken2
和PySide2
而不是shiboken
和PySide
正如下面的评论所指出的。
import shiboken
from PySide import QtGui, QtCore
import maya.OpenMayaUI as apiUI
def getMayaWindow():
"""
Get the main Maya window as a QtGui.QMainWindow instance
@return: QtGui.QMainWindow instance of the top level Maya windows
"""
ptr = apiUI.MQtUtil.mainWindow()
if ptr is not None:
return shiboken.wrapInstance(long(ptr), QtGui.QWidget)
请注意 sip
有 wrapinstance
其中 i 不是大写而是在 shiboken.wrapInstance
i 是资本。
shiboken.wrapInstance()
需要 wrapertype 作为第二个参数,因此您可以将 QWidget
作为第二个参数传递。
关于python - 如何使用 PySide 获取 Maya 主窗口指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22331337/