Qt Mac 多个菜单栏/可修改菜单栏

发布于 2024-10-10 16:30:47 字数 491 浏览 9 评论 0原文

我有一个显示多个子面板的应用程序客户希望为每个子面板显示不同的菜单。

显然,Mac 应用程序每个系统窗口只能有一个菜单栏,而且它的可修改性极低(如果有的话)。我需要删除/添加或启用/禁用菜单栏上的菜单。

我考虑过让每个子面板成为一个系统窗口,并为每个子面板附加一个菜单栏,但我没有看到任何切换到窗口菜单栏的规定。此外,我怀疑这样做会给子面板造成状态/定位混乱。

我的发现

我发现,如果我将操作创建为主窗口的子窗口,我可以在菜单本身中随意添加和删除它们。所以,我可以修改菜单内容,但无法修改菜单栏内容。

我发现我还可以随时将菜单标题更改为任何内容。因此,如果我清除内容并将标题设置为空字符串,则具有删除菜单的明显效果(尽管它仍然存在并且仍然突出显示)。

除非有其他解决方案,目前必须这样做。

这在 Mac 上可能吗?如果我进入 Cocoa(不知道 Cocoa),我是否能够设置多个菜单栏,或者至少在子面板更改时修改菜单栏?

I have an application that shows multiple subpanels & the client wants to show different menus for each subpanel.

Mac apps can only have one menuBar per system window, apparently, and it's minimally modifiable (if at all.) I need to remove/add or enable/disable menus on the menubar.

I've thought about making each of the subpanels a system window and attaching a menubar to each, but I don't see any provision for switching to a window's menubar. Besides, I suspect that doing so would create a state/positioning mess for the subpanels.

What I've Found

I've found that if I create the actions as children of the main window, I can add and remove them at will from the menus themselves. So, I can modify the menu contents, but I can't modify the menubar contents.

I've found I can also change the title of the menu to anything at any time. So, if I clear the contents and set the title to an empty string, it has the apparent effect of removing the menu (although it's still there and still highlights).

Barring another solution, have to do that, for now.

Is this possible at all on Mac? If I went down into Cocoa (don't know Cocoa), would I be able to maybe set up multiple menubars, or at least modify the menubar when the subpanel changes?

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

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

发布评论

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

评论(3

衣神在巴黎 2024-10-17 16:30:47

我正在寻找其他东西,但正如我刚刚一直在研究这个一样,我所做的是

- delete the current menuBar if there's one
- menuBar=new QMenuBar(0);
- menuBar->setNativeMenuBar(true);

而且它似乎工作得很好。只是为了它的价值。

I was searching something else but as precisely I've just been working this one, what I do is

- delete the current menuBar if there's one
- menuBar=new QMenuBar(0);
- menuBar->setNativeMenuBar(true);

And it seems to work fine. Just for what it's worth.

一生独一 2024-10-17 16:30:47

Cocoa 应用程序在任何给定时间都只有一个活动菜单栏,您可以修改和替换它。例如,下面的 (Cocoa) 代码向菜单栏添加一个新菜单(包含三个项目)。还可以编辑和删除菜单以及菜单项。

NSMenu *menubar = [NSApp mainMenu];

NSMenuItem *newBarMenuItem = [[[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""] autorelease];
NSMenu *newMenu = [[[NSMenu alloc] initWithTitle:@"New Menu"] autorelease];

NSMenuItem *menuItem1 = [[[NSMenuItem alloc] initWithTitle:@"Action 1" action:@selector(action1:) keyEquivalent:@""] autorelease];
NSMenuItem *menuItem2 = [[[NSMenuItem alloc] initWithTitle:@"Action 2" action:@selector(action2:) keyEquivalent:@""] autorelease];

[newMenu addItem:menuItem1];
[newMenu addItem:[NSMenuItem separatorItem]];
[newMenu addItem:menuItem2];

[menubar addItem:newBarMenuItem];
[menubar setSubmenu:newMenu forItem:newBarMenuItem];

[NSApp mainMenu] 返回应用程序菜单。主菜单/菜单栏中添加了一个新菜单项,代表一个包含三个项目的子菜单,其中之一是分隔符。

menu

还可以通过制作适当的菜单并发送 [NSApp setMainMenu:menubarReplacement]< 来替换菜单栏/代码>。

A Cocoa application has only one menubar active at any given time, and you can modify and replace it. For instance, the (Cocoa) code below adds a new menu (with three items) to the menubar. It is also possible to edit and remove menus as well as menu items.

NSMenu *menubar = [NSApp mainMenu];

NSMenuItem *newBarMenuItem = [[[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""] autorelease];
NSMenu *newMenu = [[[NSMenu alloc] initWithTitle:@"New Menu"] autorelease];

NSMenuItem *menuItem1 = [[[NSMenuItem alloc] initWithTitle:@"Action 1" action:@selector(action1:) keyEquivalent:@""] autorelease];
NSMenuItem *menuItem2 = [[[NSMenuItem alloc] initWithTitle:@"Action 2" action:@selector(action2:) keyEquivalent:@""] autorelease];

[newMenu addItem:menuItem1];
[newMenu addItem:[NSMenuItem separatorItem]];
[newMenu addItem:menuItem2];

[menubar addItem:newBarMenuItem];
[menubar setSubmenu:newMenu forItem:newBarMenuItem];

[NSApp mainMenu] returns the application menu. A new menu item is added to the main menu/menubar, representing a submenu that contains three items, one of them being a separator.

menu

It is also possible to replace the menubar by crafting an appropriate menu and sending [NSApp setMainMenu:menubarReplacement].

云巢 2024-10-17 16:30:47

是的,这在 Qt 应用程序中是可能的,而且相当常见。 :)

在您的应用程序中,您可能有代码来构建菜单,并首先将它们安装到菜单栏中(使用 QMenuBar)。正如胡安正确指出的那样,要更改菜单栏,您可以删除该实例并根据需要重新生成新的菜单栏及其菜单。

在我自己的代码中,我只是保留原来的 QMenuBar,并在实例上调用 QMenuBar::clear() 。这是 Juan 建议的删除/重新实例化的替代方法,尽管这两种方法都可能有效。然后我用当前需要的菜单重新填充菜单栏。

我通常只在顶级菜单集或顶级菜单的标题需要更改时重建整个 QMenuBar。更常见的是,我在给定菜单中动态重新生成实际菜单项(QActions)和/或它们的状态(例如它们的文本,无论它们是否启用、选中或未选中等)。

要仅动态重新生成单个菜单的内容,您可以将方法回调连接到该特定 QMenu 的 aboutToShow 信号,并在该回调中动态重建菜单项(不要忘记在实例上从 QMenu::clear() 开始,否则您最终可能会在菜单中出现重复的项目!)。当 QMenu 弹出时,它将显示您动态重建的项目/状态。此方法也适用于弹出/上下文菜单的动态重新生成。

Yes, this is possible in a Qt app, and fairly common. :)

In your app you probably have code to build your menus, and install them into the menubar (using QMenuBar) in the first place. As Juan correctly points out, to alter the menu bar, you can delete that instance and regenerate a new menubar and its menus as needed.

In my own code, I just keep my original QMenuBar around, and call QMenuBar::clear() on the instance. This is an alternative to the delete/re-instantiate that Juan recommends, although either approach is likely valid. Then I repopulate the menubar with the currently needed menus.

I typically only rebuild the whole QMenuBar when the set of top-level menus, or the title of a top-level menu needs to change. More commonly, I am dynamically regenerating the actual menu items (QActions) and/or their state (like their text, whether they are enabled or not, checked or not, etc) within a given menu.

To dynamically regenerate a single menu's contents only, you can connect a method callback to that particular QMenu's aboutToShow signal, and rebuild the menu's items dynamically within that callback (don't forget to start with QMenu::clear() on the instance or you may end up with duplicate items in the menu!). When the QMenu pops up, it will show your dynamically rebuilt items/states. This method also works for dynamic regeneration of popup/context menus.

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