c++ - 如何在qt中包含菜单项的工具提示

标签 c++ qt qmenu qtooltip

我正在尝试使用工具属性在 MenuBar 项目上添加工具提示,但它不起作用......但在标签、按钮和其他小部件上,它似乎工作得很好。谁能帮我这个?

最佳答案

由于缺少 MCVE ,我准备了自己的并且能够重现OP的问题。
(我在 Windows/VS2017/Qt 5.13 和 cygwin/X11/Qt 5.9 中进行了测试。)

上网查了一下,在Qt论坛上发现了一个类似的Q/A:

(Solved) setToolTip in QAction menu .

由于我已经有了 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以及。
  • 关于c++ - 如何在qt中包含菜单项的工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60484006/

    相关文章:

    qt - QTableWidgetItem如何设置单元格边框和背景色?

    qt - 创建一个 QPainter 对象并在 PaintEvent 中使用它

    c++ - 即使 QTabletEvent 被接受,QWidget 的 mousePressEvent 也会被调用

    macos - 在 Mac 上打开 QMenu 时,如何向其动态添加操作?

    python - pyqt 动态生成 QMenu Action 并连接

    c++ - 存在调试符号,但 eclipse 没有将它们与源代码相关联

    c++ - 为什么编译不会失败?

    c++ - C++中的递归(生成二进制代码)

    c++ - Visual C++ 是否提供与 GCC 中的 `__attribute__((alias))` 具有相同功能的语言结构?

    c++ - Qt & Prepending 一个 QMenu 到另一个 QMenu