为什么 QToolTips 不会出现在 QMenu 中的 QActions 上 [英] Why won't QToolTips appear on QActions within a QMenu
问题描述
我正在用 PySide
编写一个带有 GUI 的应用程序.我在 QPushButton
上设置了一个 QMenu
,通过 QMenu.addAction
添加了几个 QActions
.为了进一步向用户解释这些操作,我使用 QAction.setToolTip
添加了 QToolTip
到这些操作.
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
.
当我现在运行 GUI 时,我的 QToolTip
不会显示.下面发布的示例重现了同样的问题,有什么想法吗?
When I run the GUI now my QToolTip
won't show. The example posted below reproduces the same issue, any ideas?
提前致谢
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()
推荐答案
在 Qt-5.1 或更高版本中,您可以简单地使用 QMenu.setToolTipsVisible,菜单项将按预期显示其工具提示(请参阅 QTBUG-13663):
In Qt-5.1 or later, you can simply use QMenu.setToolTipsVisible, and the menu items will show their tooltips as expected (see QTBUG-13663):
menu.setToolTipsVisible(True)
但是,对于 Qt-4.* 和 Qt-5.0,情况就不同了.如果一个动作被添加到工具栏,它的工具提示将显示;但是如果将相同的操作添加到 QMenu
中,则不会,并且没有内置 API 可以更改它.有几种方法可以解决这个问题.一种是使用 状态提示,这将显示状态栏中的菜单项信息.另一种是使用 QMenu 自己实现菜单项工具提示功能.hovered 信号和 QToolTip.showText:
However, for Qt-4.* and Qt-5.0, the situation is different. If an action is added to a toolbar, its tooltip will be shown; but if the same action is added to a QMenu
, it won't be, and there is no built-in API to change that. There are a couple of ways to work around this. One is to use status tips instead, which will show the menu-item information in the status-bar. The other is to implement the menu-item tooltip functionality 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))
这篇关于为什么 QToolTips 不会出现在 QMenu 中的 QActions 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!