如何使用PySide获取maya主窗口指针?

 mobiledu2502856483 发布于 2023-01-18 17:32

我在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 # 

谁知道出了什么问题?

1 个回答
  • 您需要导入shiboken而不是sip传递QWidgetwrapInstanceQObject而不是QObject

    编辑: Maya2017包含shiboken2PySide2替代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)
    

    请注意,sipwrapinstance是不是资本,但在shiboken.wrapInstance 的资本.

    shiboken.wrapInstance()需要包装类型作为第二个参数,因此您可以QWidget作为第二个参数传递.

    2023-01-18 17:35 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有