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?
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)
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.
I am definitely not opposed to some PySide2 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
I get that 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!
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 )
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 Thank you!
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