Skip to content

Commit

Permalink
Add InstantTooltipEventFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
zkovari committed Feb 21, 2022
1 parent 98f9ecd commit 9feb688
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from qtpy.QtWidgets import QMainWindow, QApplication, QWidget

from qthandy import underline, bold, vbox, btn_popup_menu, ask_confirmation
from qthandy.filter import InstantTooltipEventFilter


class MainWindow(QMainWindow):
Expand All @@ -19,6 +20,8 @@ def __init__(self, parent=None):
bold(self.lbl)

self.btnWithMenu = QPushButton('Btn with menu')
self.btnWithMenu.setToolTip('Test tooltip')
self.btnWithMenu.installEventFilter(InstantTooltipEventFilter(self.btnWithMenu))

menu = QMenu(self.btnWithMenu)
menu.addAction('Test', lambda: ask_confirmation('Test'))
Expand Down
18 changes: 18 additions & 0 deletions qthandy/filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from qtpy.QtCore import QObject, QEvent
from qtpy.QtGui import QCursor
from qtpy.QtWidgets import QWidget, QToolTip


class InstantTooltipEventFilter(QObject):
def __init__(self, parent=None):
super(InstantTooltipEventFilter, self).__init__(parent)

def eventFilter(self, watched: QObject, event: QEvent) -> bool:
print('event')
if isinstance(watched, QWidget) and event.type() == QEvent.Enter:
print('show')
QToolTip.showText(QCursor.pos(), watched.toolTip())
elif event.type() == QEvent.Leave:
QToolTip.hideText()

return super(InstantTooltipEventFilter, self).eventFilter(watched, event)
15 changes: 15 additions & 0 deletions test/test_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from qtpy.QtWidgets import QPushButton, QToolTip

from qthandy.filter import InstantTooltipEventFilter


def test_instant_tooltip(qtbot):
btn = QPushButton('Button')
qtbot.addWidget(btn)
btn.show()

btn.setToolTip('Test button')
btn.installEventFilter(InstantTooltipEventFilter(btn))
qtbot.mouseMove(btn)

assert QToolTip.isVisible()

0 comments on commit 9feb688

Please sign in to comment.