我在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 "", line 4, in # TypeError: wrapinstance() argument 2 must be sip.wrappertype, not Shiboken.ObjectType #
谁知道出了什么问题?
您需要导入shiboken
而不是sip
传递QWidget
给wrapInstance
QObject而不是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
在我是不是资本,但在shiboken.wrapInstance
我的资本.
shiboken.wrapInstance()
需要包装类型作为第二个参数,因此您可以QWidget
作为第二个参数传递.