Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Tooltip on QAction
Forum Updated to NodeBB v4.3 + New Features

[Solved] Tooltip on QAction

Scheduled Pinned Locked Moved General and Desktop
9 Posts 6 Posters 10.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    koahnig
    wrote on last edited by
    #1

    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?

    Vote the answer(s) that helped you to solve your issue(s)

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexisdm
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        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.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • P Offline
          P Offline
          phlucious
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            phlucious
            wrote on last edited by
            #5

            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());
            }
            }@

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Saurabh Raj
              wrote on last edited by
              #6

              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);

              1 Reply Last reply
              0
              • I Offline
                I Offline
                iTrooz
                wrote on last edited by
                #7

                Unfortunately, this doesn't work if the QAction is disabled ):

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  j-hap
                  wrote on last edited by
                  #8

                  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);
                  }
                  
                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    j-hap
                    wrote on last edited by
                    #9

                    nevermind, you just need to call setToolTipsVisible(true) and it works even for disabled actions. only the redraw behavior of actions with the same tooltip (if e.g. disabled for the same reason) is a bit weird

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved