gpt4 book ai didi

c++ - 如何在 qt 中添加一个条目到工具栏上下文菜单?

转载 作者:太空狗 更新时间:2023-10-29 23:49:41 30 4
gpt4 key购买 nike

默认情况下,工具栏的上下文菜单中会填入工具栏的名称。我想通过一个额外的条目来扩展这个上下文菜单。

我找到了一个扩展 QTextEdit 元素的上下文菜单的示例。

http://www.qtcentre.org/threads/35166-extend-the-standard-context-menu-of-qtextedit

但是,它使用了 QTextEdit 类的 createStandardContextMenu。但是 QToolBar 似乎没有那个属性:

http://doc.qt.io/qt-4.8/qtoolbar.html

编辑

显然,默认的上下文菜单是来自 QMainWindow 的菜单。

http://doc.qt.io/qt-4.8/qmainwindow.html#createPopupMenu

不幸的是,我还不知道如何向其中添加条目。

编辑

我正在使用这个来源:

http://doc.qt.io/qt-5/qtwidgets-mainwindows-application-example.html

最佳答案

如果您想为主窗口中的所有QToolBar 提供相同的上下文菜单,则无需派生QToolBar,只需覆盖createPopupMenu()在主窗口中将自定义操作添加到返回的菜单中,如下所示:

QMenu* MainWindow::createPopupMenu(){
//call the overridden method to get the default menu containing checkable entries
//for the toolbars and dock widgets present in the main window
QMenu* menu= QMainWindow::createPopupMenu();
//you can add whatever you want to the menu before returning it
menu->addSeparator();
menu->addAction(tr("Custom Action"), this, SLOT(CustomActionSlot()));
return menu;
}

关于c++ - 如何在 qt 中添加一个条目到工具栏上下文菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38035921/

30 4 0