Max Belli Max Belli - 23 days ago 9
Python Question

Why won't QToolTips appear on QActions within a QMenu

I'm doing an app with in GUI written with

PySide
. I set a
QMenu
on a
QPushButton
, added several
QActions
via
QMenu.addAction
. To further explain these actions to the user I added
QToolTip
's to these with
QAction.setToolTip
.

When I run the GUI now my
QToolTip
won't show. The example posted below reproduces the same issue, any ideas?

Thanks in advance

import sys
from PySide import QtGui

class Example(QtGui.QPushButton):

def __init__(self, parent = None):
super(Example, self).__init__(parent)

self.setText('TestMenu')
self.setToolTip('This is a Test Button')

menu = QtGui.QMenu(self)
action_1 = menu.addAction('Action1')
action_1.setToolTip('This is action 1')
action_2 = menu.addAction('Action2')
action_2.setToolTip('This is action 2')
action_3 = menu.addAction('Action3')
action_3.setToolTip('This is action 3')
action_4 = menu.addAction('Action4')
action_4.setToolTip('This is action 4')

self.setMenu(menu)
self.show()

def main():
app = QtGui.QApplication(sys.argv)
ex = Example()

app.exec_()

if __name__ == '__main__':
main()

Answer

If an action is added to a QMenu, its tooltip won't be shown. However, if the same action is added to a toolbar, its tooltip will be shown.

This is due to differences in how QMenu renders its menu items, and is not a bug.

If you want to show information about a menu item, you can use status tips instead. But if you really must have tooltips for menu items, you can implement something yourself using the QMenu.hovered signal and QToolTip.showText:

        self.menu = QtGui.QMenu(self)
        ...
        self.menu.hovered.connect(self.handleMenuHovered)

    def handleMenuHovered(self, action):
        QtGui.QToolTip.showText(
            QtGui.QCursor.pos(), action.toolTip(),
            self.menu, self.menu.actionGeometry(action))