Is there a way to get tooltips for maya menItem?

Hey, first time poster here :smiley:

I’m trying to build a menu bar for a collection of scripts I’ve done and hit a snag while looking for a way to add tooltips to the child-items added onto the menu. I’ve tried annotations but can’t seem to get them to work. Is there a way to add any kind of tooltips the menuItem?

Thank you!

Depends if you want to just use the maya menuItem or if you can/want to go down the PySide2 route.
I don’t think the menuItem one has it but if you are making and adding your own menu to the Maya menuBar then you can technically add a tooltip via the setToolTip function on that QAction

from PySide2 import QtWidgets
import sys
from maya import OpenMayaUI as omui 
from shiboken2 import wrapInstance 

# get mainwindow from Maya
# NOTE: most code will wrap this in a QWidget, but then you can't "directly" get the menubar, this is 
# just an easier/quicker way. It's probably still better to get QWidget and then find the menubar.
def get_maya_main_win():
    mayaMainWindowPtr = omui.MQtUtil.mainWindow()
    if sys.version_info.major >= 3:
        mayaMainWindow = wrapInstance(int(mayaMainWindowPtr), QtWidgets.QMainWindow)
    else:
        mayaMainWindow = wrapInstance(long(mayaMainWindowPtr), QtWidgets.QMainWindow)
    return mayaMainWindow

maya_main_win = get_maya_main_win()

# create custom menu
custom_menu = QtWidgets.QMenu("MyMenu", parent=maya_main_win)
action1 = QtWidgets.QAction("TestAction" )
action1.setToolTip("This is a tooltip")
custom_menu.addAction(action1)

menuBar = maya_main_win.menuBar()
menuBar.addMenu(custom_menu)
2 Likes

If you’re doing this in native Maya cmds then if I remember correctly when adding your menuItem’s you can use the docTag flag to add an ID onto the item that you can then use later on to search for, and ID that menu. I seem to remember using these years back when we needed to get items added to a shelf reliably.

Been 10yrs since I did this mind you :wink:

1 Like

Hmm, interesting :smiley: I’ll check it out and see if I can make it work. Thank you so much for the tip!

I am definitely not opposed to some PySide2 :smiley: Might just re-write it all if I can make it work. Thank you so much for the example and the tip! Really appreciate it :slight_smile:

No problem! Where I can avoid dcc built in stuff I use pyside as much as I can haha, as it’s just more maintainable usually.

2 Likes

I get that :slight_smile: It also seems a lot more flexible. I’ve slowly tried to learn/migrate over to it whenever I feel like I can, and this has certainly won me over. All worked like a charm! :smiley:

1 Like

Note that by default, ToolTip visibility is disabled and ToolTipDuration is set to: (-1).

For example:
Requests:

custom_menu.ToolTipVisible()
# Result: False
custom_menu.ToolTipDuration()
# result: -1

Enable ToolTip visibility:

custom_menu.setToolTipVisible(1)

The history of this situation:
https://bugreports.qt.io/browse/QTBUG-13663

Also, be aware that PySide code is very poorly compatible between different versions, even within PySide2.
For example, the menu code sample will not work in Maya 2019.
The difference in code and syntax approaches will be something like between OM and OM2 (although Maya 2019 uses PySide2(Maya 2019: 5.6.1, Maya2020-23: 5.12.5) and shiboken2 :slight_smile: )

The change to make it work in earlier versions is pretty much just py2 for the getting the window…

Tbf, most of the dccs work pretty well and if you are concerned you can just use Qt.py to get the other bit of the way.
I’ve never had to enable the tooltip either. It makes sense for it to be false if there isn’t any yet…

Makes sense that it’s set by that there’s a flag and that it’s initially set to to false. Thank you for the heads up, both about the flag and the compatibility issues that might arise from pyside. I don’t think it’s going to be an issue for this instance, but it’s a good thing to have in the back of my mind in case things don’t work :slight_smile: Thank you!

maya has tooltips disabled. but you can get the qt menubar and enable it.
note most default maya menu items already have tooltips :smiley:
image

from PySide2 import QtWidgets

main_window = None
for widget in QtWidgets.QApplication.topLevelWidgets():
    if type(widget) == QtWidgets.QMainWindow:
        main_window = widget
        break
print(main_window.findChild(QtWidgets.QMenuBar))
menu_bar = main_window.findChild(QtWidgets.QMenuBar)

# print(menu_bar.children())
for action in menu_bar.actions() :
    print(action)
    print(action.menu())
    action.menu().setToolTipsVisible(True)

you can get the menu-items from your menu and set their tooltip

    for action2 in action.menu().actions():
        action2.setToolTip("bob")

if you name your maya menu items, action.objectName() will return the name. a good practice to identify your menu items.

1 Like

Thank you all for the help C: Managed to get the action.setToolTip(“This is a tooltip”) to work after enabling tool tips in Mayas preferences. Added mel.eval(“help -popupMode true;”) to do so directly in the tool as well :smiley: