[英]How to include tooltips for menu items in qt
我正在嘗試使用工具屬性在 MenuBar 項目上添加工具提示,但它不起作用......但在標簽、按鈕和其他小部件上,它似乎工作得很好。 誰能幫我這個?
由於缺少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)
在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)
筆記:
我對我復制的答案進行了一些微調(例如添加缺少的返回語句)。
在擺弄我的示例時,我意識到子菜單和復制/粘貼編程的解決方案QMenu
也存在同樣的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.