首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在qt中包含菜单项的工具提示

如何在qt中包含菜单项的工具提示
EN

Stack Overflow用户
提问于 2020-03-02 07:05:00
回答 1查看 883关注 0票数 5

我试图使用工具属性在MenuBar项上添加MenuBar,但是它没有工作.但在标签、按钮和其他小部件上,它似乎运行得很好。有人能帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-02 08:57:18

由于缺乏MCVE,我准备了自己的操作系统,并能够复制OP的问题。(我用Windows/Qt 2017/QT5.13和cygwin/X11/QT5.9进行了测试。)

在研究网络时,我在Qt论坛上发现了类似的问答:

(已解决) setToolTip在QAction菜单中的应用

由于我已经有了一个MCVE,我尝试了这个解决方案,并使它工作(在Windows/ As 2017/QT5.13中)。

testQMenuBarToolTip.cc

代码语言:javascript
代码运行次数:0
运行
复制
// 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

代码语言:javascript
代码运行次数:0
运行
复制
SOURCES = testQMenuBarToolTips.cc

QT += widgets

输出:(Windows 10,VS2017,QT5.13)

cygwin64中构建并测试了:

代码语言:javascript
代码运行次数:0
运行
复制
$ 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++,QT5.9)

备注:

  • 我对复制的答案进行了一些微调(例如添加缺失的返回语句)。
  • 当我摆弄我的样本时,我意识到了子菜单和复制/粘贴的问题,也为QMenu编写了解决方案。
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60484006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档