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. Display tooltip on submenu
Forum Updated to NodeBB v4.3 + New Features

Display tooltip on submenu

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 2.0k Views 2 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.
  • D Offline
    D Offline
    deleted252
    wrote on last edited by
    #1

    I'm trying to display a tooltip on a submenu of type QMenu, i.e. have the following structure:

    • Main-Menu (QMenu)
      • Foo (QAction)
      • Bar (QAction)
      • Sub-Menu (QMenu; display tooltip here when hovering)
        • Foo2 (QAction)
        • Bar2 (QAction)
      • Another-Sub-Menu (QMenu; uninteresting)
      • ...

    First I tried the following code

    Main-Menu->setToolTipVisible(true);
    Sub-Menu->setToolTip("test");
    

    but that obviously displays the tooltip for it's actions rather than itself (i.e. tooltip will be shown when hovering over Foo2 and Bar2 rather than when hovering over Sub-Menu) so then I tried using connect like so

        connect(Sub-Menu, &QMenu::hovered, Main-Menu, [=] {
            QToolTip::showText(Sub-Menu->pos(), "this is a test");
        });
    

    but that didn't work either (same problem as above). I was wondering - being new to the Qt Framework - if it is possible to display a tooltip on a submenu in the first place and if so, how?

    B 1 Reply Last reply
    0
    • D deleted252

      I'm trying to display a tooltip on a submenu of type QMenu, i.e. have the following structure:

      • Main-Menu (QMenu)
        • Foo (QAction)
        • Bar (QAction)
        • Sub-Menu (QMenu; display tooltip here when hovering)
          • Foo2 (QAction)
          • Bar2 (QAction)
        • Another-Sub-Menu (QMenu; uninteresting)
        • ...

      First I tried the following code

      Main-Menu->setToolTipVisible(true);
      Sub-Menu->setToolTip("test");
      

      but that obviously displays the tooltip for it's actions rather than itself (i.e. tooltip will be shown when hovering over Foo2 and Bar2 rather than when hovering over Sub-Menu) so then I tried using connect like so

          connect(Sub-Menu, &QMenu::hovered, Main-Menu, [=] {
              QToolTip::showText(Sub-Menu->pos(), "this is a test");
          });
      

      but that didn't work either (same problem as above). I was wondering - being new to the Qt Framework - if it is possible to display a tooltip on a submenu in the first place and if so, how?

      B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #6

      @volsa said in Display tooltip on submenu:

      Sub-Menu->setToolTip("test");

      This will set tooltip to the menu widget, not the "submenu" action in the main menu.

      To set tooltip to the "submenu" action:

      Sub-Menu->menuAction()->setToolTip("test");
      

      But it is really hard to see the tooltip during my testing on Windows. Sometimes you can see it, most times you can't...
      I think it is because the "hover" event need to handle the submenu showing thing prior to the tooltip showing thing...

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi and welcome to devnet,

        Which version of Qt ?
        Which OS ?
        Can your provide a minimal compilable example that shows the behaviour ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • D Offline
          D Offline
          deleted252
          wrote on last edited by deleted252
          #3

          Hi @SGaist,
          here's a quick and dirty example:

          #include <QApplication>
          #include <QObject>
          #include <QToolTip>
          #include <QtWidgets/QMainWindow>
          #include <QtWidgets/QMenu>
          #include <QtWidgets/QMenuBar>
          
          int main(int argc, char *argv[]) {
              QApplication application(argc, argv);
              QMainWindow mainWindow;
              QMenuBar menuBar;
          
              QMenu mainMenu("Main");
              QMenu subMenu("Sub");
              QAction actionA("A");
              QAction actionB("B");
              QAction actionC("C");
              QAction actionD("D");
          
              menuBar.addMenu(&mainMenu);
              mainMenu.addAction(&actionA);
              mainMenu.addAction(&actionB);
              mainMenu.addMenu(&subMenu);
              subMenu.addAction(&actionC);
              subMenu.addAction(&actionD);
              subMenu.setEnabled(false);
          
          
              // doesn't work, will display "tooltip test" on actions (C and D, when enabled)
              //    mainMenu.setToolTipsVisible(true);
              //    subMenu.setToolTip(QString("Tooltip test"));
          
              // does work to some extent, but "tooltip test" will now be shown
              // when hovering over A, B, C, D or Sub
              QObject::connect(&mainMenu, &QMenu::hovered, &subMenu, [&mainMenu]() {
                  QToolTip::showText(mainMenu.pos(), "tooltip test");
              });
          
          
              mainWindow.setMenuBar(&menuBar);
              mainWindow.resize(500, 500);
              mainWindow.show();
          
              return application.exec();
          }
          
          

          and the corresponding CMake:

          cmake_minimum_required(VERSION 3.17)
          project(cpp_playground)
          
          set(CMAKE_CXX_STANDARD 17)
          
          find_package(Qt5 COMPONENTS Widgets REQUIRED)
          
          add_executable(cpp_playground main.cpp)
          target_link_libraries(cpp_playground Qt5::Widgets)
          

          All I'm trying is to show the tooltip on the disabled QMenu called "Sub" to give some infos as to why it's disabled in the first place, but albeit no luck. Qt version 5.12.8 running on Ubuntu 20.04

          JonBJ 1 Reply Last reply
          0
          • D deleted252

            Hi @SGaist,
            here's a quick and dirty example:

            #include <QApplication>
            #include <QObject>
            #include <QToolTip>
            #include <QtWidgets/QMainWindow>
            #include <QtWidgets/QMenu>
            #include <QtWidgets/QMenuBar>
            
            int main(int argc, char *argv[]) {
                QApplication application(argc, argv);
                QMainWindow mainWindow;
                QMenuBar menuBar;
            
                QMenu mainMenu("Main");
                QMenu subMenu("Sub");
                QAction actionA("A");
                QAction actionB("B");
                QAction actionC("C");
                QAction actionD("D");
            
                menuBar.addMenu(&mainMenu);
                mainMenu.addAction(&actionA);
                mainMenu.addAction(&actionB);
                mainMenu.addMenu(&subMenu);
                subMenu.addAction(&actionC);
                subMenu.addAction(&actionD);
                subMenu.setEnabled(false);
            
            
                // doesn't work, will display "tooltip test" on actions (C and D, when enabled)
                //    mainMenu.setToolTipsVisible(true);
                //    subMenu.setToolTip(QString("Tooltip test"));
            
                // does work to some extent, but "tooltip test" will now be shown
                // when hovering over A, B, C, D or Sub
                QObject::connect(&mainMenu, &QMenu::hovered, &subMenu, [&mainMenu]() {
                    QToolTip::showText(mainMenu.pos(), "tooltip test");
                });
            
            
                mainWindow.setMenuBar(&menuBar);
                mainWindow.resize(500, 500);
                mainWindow.show();
            
                return application.exec();
            }
            
            

            and the corresponding CMake:

            cmake_minimum_required(VERSION 3.17)
            project(cpp_playground)
            
            set(CMAKE_CXX_STANDARD 17)
            
            find_package(Qt5 COMPONENTS Widgets REQUIRED)
            
            add_executable(cpp_playground main.cpp)
            target_link_libraries(cpp_playground Qt5::Widgets)
            

            All I'm trying is to show the tooltip on the disabled QMenu called "Sub" to give some infos as to why it's disabled in the first place, but albeit no luck. Qt version 5.12.8 running on Ubuntu 20.04

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @volsa
            You probably know more about this than I, but did you see https://stackoverflow.com/questions/27161122/qtooltip-for-qactions-in-qmenu and the code in the accepted solution there, which looks like it's doing tooltips on QMenu rather than ion QAction?

            D 1 Reply Last reply
            0
            • JonBJ JonB

              @volsa
              You probably know more about this than I, but did you see https://stackoverflow.com/questions/27161122/qtooltip-for-qactions-in-qmenu and the code in the accepted solution there, which looks like it's doing tooltips on QMenu rather than ion QAction?

              D Offline
              D Offline
              deleted252
              wrote on last edited by
              #5

              @JonB Isn't that exactly the opposite, namely show tooltips on QActions?

              JonBJ 1 Reply Last reply
              0
              • D deleted252

                I'm trying to display a tooltip on a submenu of type QMenu, i.e. have the following structure:

                • Main-Menu (QMenu)
                  • Foo (QAction)
                  • Bar (QAction)
                  • Sub-Menu (QMenu; display tooltip here when hovering)
                    • Foo2 (QAction)
                    • Bar2 (QAction)
                  • Another-Sub-Menu (QMenu; uninteresting)
                  • ...

                First I tried the following code

                Main-Menu->setToolTipVisible(true);
                Sub-Menu->setToolTip("test");
                

                but that obviously displays the tooltip for it's actions rather than itself (i.e. tooltip will be shown when hovering over Foo2 and Bar2 rather than when hovering over Sub-Menu) so then I tried using connect like so

                    connect(Sub-Menu, &QMenu::hovered, Main-Menu, [=] {
                        QToolTip::showText(Sub-Menu->pos(), "this is a test");
                    });
                

                but that didn't work either (same problem as above). I was wondering - being new to the Qt Framework - if it is possible to display a tooltip on a submenu in the first place and if so, how?

                B Offline
                B Offline
                Bonnie
                wrote on last edited by
                #6

                @volsa said in Display tooltip on submenu:

                Sub-Menu->setToolTip("test");

                This will set tooltip to the menu widget, not the "submenu" action in the main menu.

                To set tooltip to the "submenu" action:

                Sub-Menu->menuAction()->setToolTip("test");
                

                But it is really hard to see the tooltip during my testing on Windows. Sometimes you can see it, most times you can't...
                I think it is because the "hover" event need to handle the submenu showing thing prior to the tooltip showing thing...

                1 Reply Last reply
                2
                • D deleted252

                  @JonB Isn't that exactly the opposite, namely show tooltips on QActions?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @volsa said in Display tooltip on submenu:

                  @JonB Isn't that exactly the opposite, namely show tooltips on QActions?

                  I thought because the code has if (helpEvent->type() == QEvent::ToolTip && activeAction() != 0) you would try it with && activeAction() == 0 to see if that was your situation.

                  However, this is now moot, because @Bonnie obviously has the right answer.

                  1 Reply Last reply
                  1

                  • Login

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