Skip to content

Commit

Permalink
use QMenu.setToolTipsVisible(True) instead of the hovering workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
jborbely committed Oct 8, 2019
1 parent a3caaea commit 4556b96
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions msl/examples/qt/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def show():
app = application()
b = Button(text='My button', icon=32, icon_size=4., left_click=left, tooltip='Example button')
b.set_right_click(right)
b.add_menu_item(text='My item #1', triggered=item1, shortcut='CTRL+I', icon=43)
b.add_menu_item(text='My item #1', triggered=item1, icon=43, tooltip='Item #1')
b.add_menu_item(text='My item #2', triggered=item2, icon=47, tooltip='Second')
b.add_menu_item(text='My item #3', shortcut='CTRL+Z', icon=46, tooltip='Visible but disabled (triggered not set)')
b.add_menu_item(text='My item #3', icon=46, tooltip='Visible but disabled (triggered not set)')
b.add_menu_separator()
b.add_menu_item(text='My item #4', triggered=item4, tooltip='Fourth')
b.show()
Expand Down
18 changes: 10 additions & 8 deletions msl/qt/widgets/button.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
"""
A :class:`~QtWidgets.QToolButton` to display text, an icon and a menu.
"""
from .. import QtWidgets, QtCore, Qt, QtGui, io
from .. import (
QtWidgets,
QtCore,
Qt,
QtGui,
io,
)


class Button(QtWidgets.QToolButton):
Expand Down Expand Up @@ -94,14 +100,12 @@ def add_menu_item(self, *, text=None, triggered=None, icon=None, shortcut=None,
if text is not None:
action.setText(text)
if shortcut is not None:
action.setShortcut(shortcut)
action.setShortcut(QtGui.QKeySequence(shortcut))
if icon is not None:
action.setIcon(io.get_icon(icon))
if tooltip is not None:
action.setToolTip(tooltip)
action.setStatusTip(tooltip)
# the tooltip of a QAction in a QMenu is not shown. Here's a work-around.
self._menu.hovered.connect(self._show_menu_tooltip)
self._menu.addAction(action)

def add_menu_separator(self):
Expand Down Expand Up @@ -130,12 +134,9 @@ def set_right_click(self, fcn):
"""
self.customContextMenuRequested.connect(fcn)

def _show_menu_tooltip(self, action):
"""Slot -> The tooltip of a QAction in a QMenu is not shown. This is a work-around."""
QtWidgets.QToolTip.showText(QtGui.QCursor.pos(), action.toolTip())

def _create_menu(self):
self._menu = ButtonMenu(self)
self._menu.setToolTipsVisible(True)
self.setMenu(self._menu)
self.setPopupMode(QtWidgets.QToolButton.MenuButtonPopup)

Expand All @@ -150,6 +151,7 @@ class ButtonMenu(QtWidgets.QMenu):
"""Display the :class:`QtWidgets.QMenu` underneath the :class:`Button`."""

def showEvent(self, event):
"""Overrides :meth:`QtWidgets.QMenu.showEvent`."""
point = self.parent().mapToGlobal(QtCore.QPoint(0, 0))
offset = self.parent().width() - self.width()
self.move(point + QtCore.QPoint(offset, self.parent().height()))
Expand Down

0 comments on commit 4556b96

Please sign in to comment.