From: stack Date: Sun, 8 Jul 2012 17:59:45 +0000 (+0200) Subject: Profile menu tooltips X-Git-Url: https://v.licheni.net/stack/code/dboxswitch.git/commitdiff_plain/f19cd7df7f4f366e903c5160d6ca9eceb8555db5?ds=inline Profile menu tooltips for enabling QMenu to display tooltips I subclassed QMenu and overloaded event with a method that actually displays the tooltip, this is done in the class QMenuToolTip in the new added file qmenutooltip.py --- diff --git a/gui.py b/gui.py index 17a4e14..dd7e81e 100755 --- a/gui.py +++ b/gui.py @@ -36,6 +36,7 @@ import sys import os from PyQt4 import QtGui from PyQt4.QtCore import SIGNAL +from qmenutooltip import QMenuToolTip from apperror import AppError from settings import appconf @@ -118,7 +119,7 @@ class Gui(QtGui.QDialog): action to add a new One """ if reason in (QtGui.QSystemTrayIcon.Trigger, QtGui.QSystemTrayIcon.DoubleClick): - self.menuProfiles = QtGui.QMenu() + self.menuProfiles = QMenuToolTip() self.menuProfiles.setTitle("Profiles") #Get profiles from the ProfHandler embedded in the gui @@ -131,6 +132,8 @@ class Gui(QtGui.QDialog): #Using lambda function to pass additional arguments to the function, in this case the path of the profile receiver = lambda pr=pr: self.profileManager.activateProfile(pr) self.connect(menuItem_Profile, SIGNAL('triggered()'), receiver) + #set menu item ToolTip + menuItem_Profile.setToolTip("Activate profile: "+pr) self.menuProfiles.addAction(menuItem_Profile) diff --git a/qmenutooltip.py b/qmenutooltip.py new file mode 100644 index 0000000..486a3ad --- /dev/null +++ b/qmenutooltip.py @@ -0,0 +1,15 @@ +#!/bin/python + +from PyQt4 import QtGui, QtCore +class QMenuToolTip(QtGui.QMenu): + """ Implements a QMenu where it is possible to set a ToolTip and display it on mouse hover """ + def __init__(self, parent=None): + QtGui.QMenu.__init__(self, parent) + + def event(self, e): + if e.type() == QtCore.QEvent.ToolTip: + if not self.activeAction() is None: + QtGui.QToolTip.showText(e.globalPos(), self.activeAction().toolTip()) + else: + QtGui.QToolTip.hideText() + return QtGui.QMenu.event(self, e)