如何在qt中为菜单项添加工具提示

5

我试图使用工具属性在菜单栏项目上添加工具提示,但它似乎没有起作用... 但在标签、推按钮和其他小部件上,它似乎运作良好。是否有人可以帮助我解决这个问题?


我制作了一个MCVE来重现您的声明。在Windows/Qt 5.13和cygwin/X11/Qt 5.9中,菜单栏中的项目和菜单的工具提示都无法正常工作。 - Scheff's Cat
在QtForum中发现了一个类似的问题:(已解决)在QAction菜单中设置setToolTip。它通过派生的菜单栏通过自定义代码添加工具提示。 - Scheff's Cat
1
请提供您的代码示例、尝试或实现。 - Nikola Lukic
1
你可以使用 setToolTipsVisible(true);,参见这里 - florisla
2个回答

4
由于缺少 MCVE,我自己准备了一个,并成功地重现了OP的问题。(我测试了Windows/VS2017/Qt 5.13以及cygwin/X11/Qt 5.9。)
在网上研究后,在Qt论坛中找到了一个类似的Q/A: (已解决)QAction菜单中的setToolTip
因为我已经有了一个MCVE,所以我尝试了那个解决方案,并使其工作起来了(在Windows/VS2017/Qt 5.13中)。

testQMenuBarToolTip.cc:

// Qt header:
#include <QtWidgets>

/// menu bar with tooltips
class MenuBar: public QMenuBar {
  public:
    explicit MenuBar(QWidget *pQParent = nullptr): QMenuBar(pQParent) { }
    virtual ~MenuBar() = default;
    MenuBar(const MenuBar&) = delete;
    MenuBar& operator=(const MenuBar&) = delete;

  protected:
    virtual bool event(QEvent *pQEvent) override;
};

bool MenuBar::event(QEvent *pQEvent)
{
  // keep behavior of base class
  bool ret = QMenuBar::event(pQEvent);
  // check whether this is a help event
  if (pQEvent->type() == QEvent::ToolTip) {
    const QHelpEvent *const pQHelpEvent = (const QHelpEvent*)pQEvent;
    const QAction *pQAction = activeAction();
    if (pQAction && !pQAction->toolTip().isEmpty()) {
      QToolTip::showText(pQHelpEvent->globalPos(), pQAction->toolTip());
      return ret;
    }
  }
  QToolTip::hideText();
  return ret;
}

/// menu with tooltips
class Menu: public QMenu {
  public:
    explicit Menu(const QString &title, QWidget *pQParent = nullptr):
      QMenu(title, pQParent)
    { }
    explicit Menu(QWidget *pQParent = nullptr): QMenu(pQParent) { }
    virtual ~Menu() = default;
    Menu(const Menu&) = delete;
    Menu& operator=(const Menu&) = delete;

  protected:
    virtual bool event(QEvent *pQEvent) override;
};

bool Menu::event(QEvent *pQEvent)
{
  // keep behavior of base class
  bool ret = QMenu::event(pQEvent);
  // check whether this is a help event
  if (pQEvent->type() == QEvent::ToolTip) {
    const QHelpEvent *const pQHelpEvent = (const QHelpEvent*)pQEvent;
    const QAction *pQAction = activeAction();
    if (pQAction && !pQAction->toolTip().isEmpty()) {
      QToolTip::showText(pQHelpEvent->globalPos(), pQAction->toolTip());
      return ret;
    }
  }
  QToolTip::hideText();
  return ret;
}

// main application
int main(int argc, char **argv)
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  QApplication app(argc, argv);
  // setup GUI
  QMainWindow qWinMain;
  qWinMain.resize(320, 240);
  qWinMain.setWindowTitle("Test QMenuBar with ToolTips");
  MenuBar qMenuBar;
  QAction qCmdFile("File");
  qCmdFile.setToolTip("provides file commands.");
  Menu qMenuFile;
  QAction qCmdExit("Quit");
  qCmdExit.setToolTip("closes application.");
  qMenuFile.addAction(&qCmdExit);
  qCmdFile.setMenu(&qMenuFile);
  qMenuBar.addAction(&qCmdFile);
  qWinMain.setMenuBar(&qMenuBar);
#if 0 // comparison with toolbar
  QToolBar qToolBar;
  qToolBar.addAction(&qCmdExit);
  qWinMain.addToolBar(&qToolBar);
#endif // 0
  qWinMain.show();
  // runtime loop
  return app.exec();
}

testQMenuBarToolTip.pro:

SOURCES = testQMenuBarToolTips.cc

QT += widgets

输出:(Windows 10,VS2017,Qt 5.13)

Snapshot of testQMenuBarToolTips (Mouse over menu bar item) Snapshot of testQMenuBarToolTips (Mouse over menu item)

使用 cygwin64 构建并测试:

$ qmake-qt5 

$ make && ./testQMenuBarToolTips
g++ -c -fno-keep-inline-dllexport -D_GNU_SOURCE -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtWidgets -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -I/usr/lib/qt5/mkspecs/cygwin-g++ -o testQMenuBarToolTips.o testQMenuBarToolTips.cc
g++  -o testQMenuBarToolTips.exe testQMenuBarToolTips.o   -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread 
Qt Version: 5.9.4

输出:(cygwin64,X11,g ++,Qt 5.9)

Snapshot of testQMenuBarToolTips (Mouse over menu bar item) Snapshot of testQMenuBarToolTips (Mouse over menu item)

注意:

  • 我对从中复制的答案进行了一些微调(例如添加了缺失的返回语句)。

  • 在调整我的示例时,我意识到子菜单存在相同的问题,并编写了QMenu的解决方案。


非常抱歉回复晚了,但我不得不暂时推迟这个项目的工作... 但最终我尝试了一下,它起作用了。但现在我面临一个新问题,如果我使用这种方法,我该如何为这些子菜单添加操作... 比如关闭应用程序。 - user12991669
@temp 我该如何为这些子菜单添加操作呢? 这很简单 - 你需要连接到 QAction::triggered 信号。或者,你可以使用 QMenu::addAction() 并立即提供信号处理程序(在 Qt 中称为信号槽)。(它返回指向创建的操作的指针,之后可以用于设置工具提示。)查看 SO: how to create menu in statusbar in qt like PC start menu 来了解两种方法的示例。 - Scheff's Cat
谢谢你的帮助,你对我来说非常有帮助。 - user12991669

0
这是我如何通过在Qt Designer中设置属性来使工具提示正常工作的方法。
  • 我使用的是VS2022和Qt6.4.2_msv2019_64。
  • 在创建了一个QtWidgets项目之后,我在Qt Designer的菜单栏上添加了"File" -> "Open"。
  • 对于"Open"属性,我设置了"toolTip":打开配置文件。
  • 在对象检视器中,我选择了"menuFile"并勾选了"toolTipsVisible"。 enter image description here

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接