Qt Creator,在菜单栏中指定位置插入自定义菜单

发布于 2024-09-05 17:00:36 字数 252 浏览 3 评论 0原文

我用 Qt Creator 创建了一个菜单栏和一些菜单。必须对其中一个菜单进行编码才能使用 QActionGroup 功能。现在可以轻松地将我的自定义菜单添加到菜单栏:

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

但我的菜单将位于菜单栏的最后位置。如何在指定位置添加菜单? (例如“文件”菜单之后的第二个位置)

问候语

I have created a menu bar and some menus with Qt creator. One of the menus had to be coded to use QActionGroup features. 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? (e.g. the second place right after the File menu)

Greetings

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿童谣 2024-09-12 17:00:36

QMenuBar::insertMenuQMenu::menuAction

例如,如果要在“帮助”菜单之前的位置动态插入“打印”菜单,可以执行以下操作:

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

Use QMenuBar::insertMenu in conjunction with QMenu::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);
云雾 2024-09-12 17:00:36

如果您想在菜单栏中间添加一个子菜单,这并不是一件小事。没有直接的 API 可以做到这一点,但您可以通过操纵 QWidget 的内部操作来实现这一点(QMenu::addMenu 只是调用 QWidget::addAction(menu->menuAction()) 。

理论上,您可以操作 QMenuBar::actions(),但我从未这样做过。

当我必须处理这个问题时,我只是从另一个数据集重建了菜单(在您最喜欢的搜索引擎中查找 qmdilib)。 code> 你会看到我的解决方案)。

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()).

In theory, you can manipulate QMenuBar::actions(), but I never did it.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文