Solved Display tooltip on submenu
-
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
)
- Foo2 (
- Another-Sub-Menu (
QMenu
; uninteresting) - ...
- Foo (
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 soconnect(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?
- Main-Menu (
-
@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... -
Hi and welcome to devnet,
Which version of Qt ?
Which OS ?
Can your provide a minimal compilable example that shows the behaviour ? -
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 -
@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 onQMenu
rather than ionQAction
? -
@JonB Isn't that exactly the opposite, namely show tooltips on QActions?
-
@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... -
@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.