Search code examples
pyqtcontextmenumaya

pyqt+maya = fatal error when click to context menu


i use pyqt in autodesk maya. all work but when i try connect a context menu to my elements - maya get fatal error and closed.

import maya.OpenMayaUI as mui
import maya.api.OpenMaya as om

import sip
from PyQt4 import QtGui, QtCore, uic

#----------------------------------------------------------------------
def getMayaWindow():
    ptr = mui.MQtUtil.mainWindow()
    return sip.wrapinstance(long(ptr), QtCore.QObject)

#----------------------------------------------------------------------
form_class, base_class = uic.loadUiType('X:/tools/Maya/windows/2014/python/UI/perforceBrowserWnd.ui')

#----------------------------------------------------------------------
# main window class
#----------------------------------------------------------------------
class PerforceWindow(base_class, form_class):
    def __init__(self, parent=getMayaWindow()):
        super(base_class, self).__init__(parent)
        self.setupUi(self)

        # Popup Menu is not visible, but we add actions from above
        self.popMenu = QtGui.QMenu( self )
        self.popMenu.addAction("revert", self.on_action_revert)
        self.popMenu.addAction("submit", self.on_action_submit)

        self.filesListWgt.customContextMenuRequested.connect( self.filesListWgtMenuRequested )

    #------------------------------------------------------------------
    def filesListWgtMenuRequested(self, pos):
        self.popMenu.exec_( self.filesListWgt.mapToGlobal(pos) )

    def on_action_revert(self):
        print('on_action_revert')

    def on_action_submit(self):
        print('on_action_submit')


#----------------------------------------------------------------------
# window
#----------------------------------------------------------------------
def perforceBrowser2():    
    perforceBrowserWnd = PerforceWindow()
    perforceBrowserWnd.show()

perforceBrowser2()

dialog created in qtdesigner. i set attribute contentMenuPolicy in designer on QListWidtet. when i right click on QListWidtet or any element - i see a context menu. but if i click a menu or dismiss it - maya get fatal error enter image description here

and i see log text - function on_action_revert is called. but after that - maya crashed.

what i doing wrong?

update:

i try simple test. replace a menu to simple call a function:

replace connect to:

    self.filesListWgt.customContextMenuRequested.connect( self.on_action_revert )

def on_action_revert(self):
    print('on_action_revert')

this crash maya too


Solution

  • I tested your code along with your UI file on PyQt in Maya 2013 as well as on PySide (using a QtShim) on Maya 2014 and your code ran fine. Please check your PyQt build for Maya 2014.

    I recommend attempting to run your code using PySide on Maya 2014. To do this you do not need to change any of your code base. You just need to modify a few imports. It is worth checking out these: Take a look at this. You can use this to write code that is compatible in both PyQt and PySide. https://github.com/rgalanakis/practicalmayapython/blob/master/src/chapter5/qtshim.py

    And to load your ui file in PySide environment take a look at this article: http://www.jason-parks.com/artoftech/?p=579

    PyQt and PySide are both just python wrappers for the Qt framework. They are identical apart from a very few differences. So your code base never needs to change no matter what you use to run it in.

    P.s. But for whatever reason you are so particular for using PyQt for 2014, Please use these guides to build it: http://download.autodesk.com/us/support/files/maya_documentation/pyqtmaya2014.pdf and this one: http://around-the-corner.typepad.com/adn/2013/04/building-sip-and-pyqt-for-maya-2014.html