python - 如何使用 PySide 获取 Maya 主窗口指针?

标签 python pyqt pyside maya

我在 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 包含shiboken2PySide2 而不是shibokenPySide正如下面的评论所指出的。

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)

请注意 sipwrapinstance 其中 i 不是大写而是在 shiboken.wrapInstance i 是资本。

shiboken.wrapInstance() 需要 wrapertype 作为第二个参数,因此您可以将 QWidget 作为第二个参数传递。

关于python - 如何使用 PySide 获取 Maya 主窗口指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22331337/

相关文章:

python - QProgressDialog 的残骸挥之不去——有时

python - 以 32 位模式运行 OS X 通用二进制文件

python - libvlc "VLC is unable to open the MRL ' C :\Users\Public\Videos\Sample Videos\Wildlife. mwv'"

python - 为什么 python 2.7 的性能(剪影分数)比 3.6 更好?

python - PyQtGraph GraphicsLayout 中的边距

python - 删除 QMdiSubWindow 的图标和样式

python - 在表格的每一行旁边添加按钮

python - 自定义QTreeview展开事件方法

python - PySerial:如何在串行线路上发送 Ctrl-C 命令

python - Pandas 如何通过正则表达式从列提取到多行?