The application-level menus don't respond very well to being manipulated at
the Qt layer. If it were your own set of menus in another widget (not
application main window menus) you would have a lot more flexibility in
doing everything you want. I have menus where I do the same, responding to
right clicking of menu items to bring up small context menus, or just
having full access to all the events. Maybe it can be done effectively, but
I have not been having good results in my attempts.

So for specifically dealing with application menus, you may need to stick
with the commands module, and make use of the existing support for having
an option box next to your menu item. That option box can either do
something right away (which may not be very obvious or descriptive of its
intent) or it can bring up a menu to do actions.

from functools import partial import maya.cmds as cmdsimport maya.mel as mm
# Start the top menu
gMainWindow = mm.eval("$win=$gMainWindow")
cmds.setParent(gMainWindow)
m = cmds.menu("myRootMenu", label="Root Menu")
# Add items and submenus
m_sub1 = cmds.menuItem(label="Sub Menu", subMenu=True)
m_sub1_item1 = cmds.menuItem(label="Leaf 1")

cmds.setParent(m, menu=True)
m_item1 = cmds.menuItem(label="Leaf 2")
m_item1_opt = cmds.menuItem(optionBox=True)
# Attach a callback to the option boxdef handleMenuItem(item, *args):
    print "Do something with", item

cmds.menuItem(m_item1_opt, e=True, stp="python",
c=partial(handleMenuItem, m_item1))

​
And then for finding Qt instances of the menu or the actions, if you want
to try and play with them directly:

# finding the menu
ptr = omui.MQtUtil.findControl(m)
qm = wrapInstance(long(ptr), QtGui.QMenu)
# finding the menu item
ptr = omui.MQtUtil.findMenuItem(m_item1)
m_item1_action = wrapInstance(long(ptr), QtGui.QAction)

​
You will probably find it much easier to do as little as possible with the
application level menus and just get them to launch something that you have
full control over, like Marcus was suggesting with a custom editor.

-- justin



On Sun, Jul 13, 2014 at 12:14 AM, johan Borgström <[email protected]>
wrote:

> Hi Marcus,
>
> I have not tried that yet. To get a better understanding, I assume that if
> we use the cmds module to build menus, it creates Qt items "in the
> background" for us. I guess we need a way to reference theese objects to
> use Qt functionality?  Would I use "omui.MQtUtil.findControl(widgetStr)" or
> similar?
>
> It would be great if you could post a working snippet of how to add a menu
> to the main menu with the desired functionality.
>
> Best Regards,
> Johan
>
> On Friday, July 11, 2014 1:59:42 PM UTC+2, johan Borgström wrote:
>
>> Hi!
>>
>> I am building a basic menu populated with some menuItems. I want the user
>> to be able to add and remove menuItems to the menu. I have wrapped the menu
>> in a class and written some methods to help to add and remove menuItems. I
>> do not want to write a special ui to handle the removing of items, so my
>> idea is to check if the user holds a modifier key while clicking the
>> menuItem and if so delete it. Another idea is to use a right click popup
>> menu on the menuItem and have the option "delete item".
>> To add the command to the menuItem I use functools.partial. So to wrap it
>> up my questions are:
>>
>>
>>    - How can I add a right click popup menu to a menuItem.
>>    - How can I check if a modifier key was pressed when the menuItem was
>>    clicked.
>>
>> Best Regards,
>> Johan
>>
>>
>>  --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/c9057d58-27d0-4aa6-bae7-ac3d2d043eac%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/c9057d58-27d0-4aa6-bae7-ac3d2d043eac%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2rr6WaHtc50de_T2fTaURtWDnYFs-BK-%2B8vdApg1KPMg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to