简体   繁体   English

Qt创建者,将指定位置的自定义菜单插入菜单栏

[英]Qt creator, insert custom menu at specified place into menu bar

I have created a menu bar and some menus with Qt creator. 我用Qt创建器创建了一个菜单栏和一些菜单。 One of the menus had to be coded to use QActionGroup features. 其中一个菜单必须编码才能使用QActionGroup功能。 Now it is easy to add my custom menu to the menu bar with: 现在可以很容易地将我的自定义菜单添加到菜单栏中:

printMenu = menuBar()->addMenu(tr("&Print"));

but my menu will be in the last position of the menu bar. 但我的菜单将位于菜单栏的最后位置。 How do I add my menu at a specified place? 如何在指定位置添加菜单? (eg the second place right after the File menu) (例如,文件菜单后面的第二个位置)

Greetings 问候

Use QMenuBar::insertMenu in conjunction with QMenu::menuAction . QMenuBar :: insertMenuQMenu :: menuAction结合使用。

For example, if you want to dynamically insert the "Print" menu at the location before the "Help" menu, you can do something like this: 例如,如果要在“帮助”菜单之前的位置动态插入“打印”菜单,可以执行以下操作:

QMenu *printMenu = new QMenu(tr("&Print"));
menuBar()->insertMenu(ui->menuHelp->menuAction(), printMenu);

If you want to add a sub menu in the middle of the menubar, this is not trivial. 如果你想在菜单栏的中间添加一个子菜单,这不是一件容易的事。 There is no direct API to do this but you can probably pull that out byt manipulating the internal actions of QWidget (QMenu::addMenu just calls QWidget::addAction(menu->menuAction()) . 没有直接的API来做这个,但你可以通过操纵QWidget的内部动作来解决这个问题(QMenu :: addMenu只调用QWidget::addAction(menu->menuAction())

In theory, you can manipulate QMenuBar::actions(), but I never did it. 从理论上讲,你可以操纵QMenuBar :: actions(),但我从来没有这样做过。

When I had to do handle this problem, I just reconstructed the menu from another dataset (look in your favorite search engine for qmdilib and you will see my solution). 当我不得不处理这个问题时,我只是从另一个数据集重建菜单(在你最喜欢的搜索引擎中查找qmdilib ,你会看到我的解决方案)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM