[Solved] Tooltip on QAction
-
I am using Qt designer. In dialogs I could set the tool tip for QLineEdit and other widgets. As far as I remember there was no additional code required. Now I have a QMenuBar with QMenu and QActions all designed with Qt Designer. I have tried to use the tool tip, but it does not work.
The examples I have found show that one has to implement the event. But those menus are set up manually. Do I really have to setup teh event handling? Or do I miss something which is needed to be activated? -
It seems you can only set tool tips for submenu (QMenu), not menu items (QAction), but when the menu item is hovered, the QAction statusTip text will be displayed in the status bar, if there is one.
QAction tool tips are displayed if you add the QAction to a QToolBar.
There is no code to handle tool tips in QMenu. Since the code to show the tool tip, in QApplication, is triggered when you enter and hover a QWidget, and QMenu doesn't use sub-widgets to display the items but draws them directly (unlike QToolBar which create QToolButtons when you add the QAction to it), you would probably have to implement it manually, if you really want that feature.
-
Thanks for your reply.
statusTip I had used already and that does work. However, the whole display, the design is for, is quite small. My menus are sometimes covering the statusBar and, therefore, covering the text. That is the reason for looking for the toolTip.
It is a little confusing, when something works without almost no effort in one configuration, but not in the other. -
Extremely helpful post. Thank you for posting this. I, too, find it strange and inconvenient that my QAction tooltips appear everywhere except in QMenus, especially since Designer doesn't give easy access to statusTips. It was so unexpected that it took me months to even notice that the tooltips weren't displaying! Thanks for the workaround.
-
Since status tips aren't conveniently added to QActions in Qt Designer, I wound up adding this to my MainWindow constructor to copy my tooltips to the statustips for the QActions that also happen to be in my menu tree.
@QList<QMenu*> menulist(ui->menuBar->findChildren<QMenu*>());
for(int mnum = 0; mnum < menulist.size(); ++mnum)
{
QList<QAction*> actionlist(menulist.at(mnum)->actions());
for(int actnum = 0; actnum < actionlist.size(); ++actnum)
{
actionlist.at(actnum)->setStatusTip(actionlist.at(actnum)->toolTip());
}
}@ -
If your menu have actions & you want to add tool-tip for the respective actions , can follow these steps:
1: set tool-tip (action.settooltip("Your tool tip")) for every action.
2: write a connect statement on hovered() signal and write a slot for it.
3: in slot definition you have to write
QAction* act = static_cast<QAction*>(QObject::sender());
QToolTip::showText(QCursor::pos(),act->toolTip(),menuObject_where you have added action); -
I just had the same problem an thought I'd share my solution to show tooltips of disabled QActions to inform a user why that action is disabled:
I created a custom class that inherits from QMenu and handles hover events itself.
header:
#pragma once #include <QMenu> #include <QMouseEvent> #include <QToolTip> /** * Custom QMenu that shows tooltips for disabled actions. * * Qt's standard behavior prevents disabled actions from receiving mouse events, * which means tooltips and status tips won't be shown for disabled menu items. * This custom menu intercepts mouse move events and manually displays tooltips * for disabled actions, allowing users to understand why an action is unavailable. */ class DisabledActionsToolTipMenu : public QMenu { Q_OBJECT public: explicit DisabledActionsToolTipMenu(QWidget* parent = nullptr); explicit DisabledActionsToolTipMenu(QString const& title, QWidget* parent = nullptr); protected: /** * Intercepts events to handle tooltip display for disabled actions. */ bool event(QEvent* event) override; private: QAction* lastActionWithTooltip_ = nullptr; };and implementation:
#include "DisabledActionsToolTipMenu.h" #include <QHelpEvent> DisabledActionsToolTipMenu::DisabledActionsToolTipMenu(QWidget* parent) : QMenu(parent) {} DisabledActionsToolTipMenu::DisabledActionsToolTipMenu(QString const& title, QWidget* parent) : QMenu(title, parent) {} bool DisabledActionsToolTipMenu::event(QEvent* event) { if (event->type() == QEvent::ToolTip) { QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event); QAction* action = actionAt(helpEvent->pos()); if (action && !action->isEnabled() && !action->toolTip().isEmpty()) { // shows tooltip for disabled action QToolTip::showText(helpEvent->globalPos(), action->toolTip(), this); lastActionWithTooltip_ = action; return true; } } else if (event->type() == QEvent::MouseMove) { // hides tooltip immediately when mouse moves to a different action QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event); QAction* action = actionAt(mouseEvent->pos()); // action can be nullptr when hovering over empty menu space, which is fine // since nullptr != lastActionWithTooltip_ will trigger the tooltip hide if (lastActionWithTooltip_ && action != lastActionWithTooltip_) { QToolTip::hideText(); lastActionWithTooltip_ = nullptr; } } return QMenu::event(event); }