QAction not workng in dockWindow

qt

#1

Hi,

I’m facing a weird problem, I’m writing a UI in PySide2 and Maya 2018.
When the window is dockable it seems that I can’t access anymore to the QActions inside a QMenuBar.
Check the code below, is just a simplified test, but if you run it you will see that accessing to the QAction when is a dockable control gives the c++ already deleted error…

Any idea on how to solve it properly?
Thank you

from PySide2 import QtWidgets, QtGui, QtCore
from maya import OpenMayaUI as omui
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
from shiboken2 import wrapInstance

mayaMainWindowPtr = omui.MQtUtil.mainWindow()
mayaMainWindow = wrapInstance(long(mayaMainWindowPtr), QtWidgets.QWidget)


class TestActions(MayaQWidgetDockableMixin, QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(TestActions, self).__init__(parent)
        self.setMinimumSize(200, 200)
        
        main_layout = QtWidgets.QVBoxLayout()
        self.setLayout(main_layout)

        self.menu_bar = QtWidgets.QMenuBar()

        # This menu is also available with right clicking on the tree widget
        self.new_section = self.menu_bar.addMenu('Menu')
        self.test_action = self.new_section.addAction('Do it')
        self.test_action.setCheckable(True)
        
        self.test_check_box = QtWidgets.QCheckBox('cb')

        main_layout.addWidget(self.menu_bar)
        main_layout.addWidget(self.test_check_box)

    def try_action(self):
        self.test_action.setChecked(True)
        
    def try_checkbox(self):
        self.test_check_box.setChecked(True)

# So far all good
test_win = TestActions(mayaMainWindow)
test_win.show(dockable=True)

# I call my method for editing the check box
test_win.try_checkbox()

# But when I call this, I get
# RuntimeError: Internal C++ object (PySide2.QtWidgets.QAction) already deleted.
# But only if the window is dockable
test_win.try_action()

#2

It seems to be because you’re not creating the objects, your adding them and storing the the result of this… at least that’s what I think. This seemed to work for me.

from PySide2 import QtWidgets, QtGui, QtCore
from maya import OpenMayaUI as omui
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
from shiboken2 import wrapInstance

mayaMainWindowPtr = omui.MQtUtil.mainWindow()
mayaMainWindow = wrapInstance(long(mayaMainWindowPtr), QtWidgets.QWidget)


class TestActions(MayaQWidgetDockableMixin, QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(TestActions, self).__init__(parent)
        self.setMinimumSize(200, 200)
        
        main_layout = QtWidgets.QVBoxLayout()
        self.setLayout(main_layout)

        self.menu_bar = QtWidgets.QMenuBar()
        self.new_section = QtWidgets.QMenu("Menu")
        # This menu is also available with right clicking on the tree widget
        self.menu_bar.addMenu(self.new_section)
        self.test_action = QtWidgets.QAction('Do it', self)
        self.new_section.addAction(self.test_action)
        
        self.test_action.setCheckable(True)
        
        self.test_check_box = QtWidgets.QCheckBox('cb')

        main_layout.addWidget(self.menu_bar)
        main_layout.addWidget(self.test_check_box)

    def try_action(self):
        self.test_action.setChecked(False)
        
    def try_checkbox(self):
        self.test_check_box.setChecked(True)

# So far all good
test_win = TestActions(mayaMainWindow)
test_win.show(dockable=True)

# I call my method for editing the check box
test_win.try_checkbox()


test_win.try_action()

#3

Hey that is working!

It is still weird though, the method addAction(text) returns a QAction, and the method addMenu(title) returns a QMenu.
I don’t get what’s the real difference, but is working, so I will just accept that :smiley:

Thank you!!


#4

Haha some things never make sense :stuck_out_tongue:

No worries! Enjoy :slight_smile: