Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Maya 2017 and QToolButton popup menu not showing

Maya 2017 and QToolButton popup menu not showing

tomekp1981
Enthusiast Enthusiast
2,226 Views
1 Reply
Message 1 of 2

Maya 2017 and QToolButton popup menu not showing

tomekp1981
Enthusiast
Enthusiast

I have a piece of code which opens a window with a button, and this button has popup menu attached. It works in Maya 2016. But on Maya 2017, buttons menu doesn't show. Question, why?

 

The code:

 

# -*- coding: utf-8 -*-

try:
    from PySide.QtCore import *
    from PySide.QtGui import *
    from shiboken import wrapInstance
except ImportError:
    from PySide2.QtCore import *
    from PySide2.QtGui import *
    from PySide2.QtWidgets import *
    from shiboken2 import wrapInstance
    

from functools import partial
from maya import OpenMayaUI as omui
import maya.cmds as cmds


def getMayaWindow():
    try:
        mayaMainWindowPtr = omui.MQtUtil.mainWindow()
        mayaMainWindow = wrapInstance(long(mayaMainWindowPtr), QWidget)
    except:
        mayaMainWindow = None
    return mayaMainWindow
    
class ContextQuadMenuUI(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(149, 97)
        self.horizontalLayout = QHBoxLayout(Dialog)
        self.horizontalLayout.setSpacing(0)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        return


class ContextQuadMenu(QDialog):
    def __init__(self, parent=getMayaWindow()):

        if cmds.window('tmkContextQuadMenu', exists=True):
            cmds.deleteUI('tmkContextQuadMenu', window=True)
        wnd = cmds.window('tmkContextQuadMenu')
        ptr = omui.MQtUtil.findControl( wnd )
        wdg = wrapInstance(long(ptr), QWidget)

        super(ContextQuadMenu, self).__init__(wdg)

        self.ui = ContextQuadMenuUI()
        self.ui.setupUi(self)
        self.setAttribute(Qt.WA_DeleteOnClose)
        
        mymenu = QMenu()
        mymenu.addAction("item")
        newbutton = QToolButton()
        newbutton.setAutoRaise(True)
        newbutton.setText("blah")
        newbutton.setMenu(mymenu)
        newbutton.setPopupMode(QToolButton.InstantPopup)
        self.ui.horizontalLayout.addWidget(newbutton)

        self.show()
        
        
ContextQuadMenu()

What am I doing wrong? Is it Qt5 which makes the difference?

0 Likes
Accepted solutions (1)
2,227 Views
1 Reply
Reply (1)
Message 2 of 2

tomekp1981
Enthusiast
Enthusiast
Accepted solution

Ok, I've got it. I need to change this:

 

mymenu = QMenu()
mymenu.addAction("item")
newbutton = QToolButton()

 

to this:

 

newbutton = QToolButton()
mymenu = QMenu(newbutton)
mymenu.addAction("item")

 

and it works fine 🙂

0 Likes