Qt:如何在子窗口的默认系统菜单中显示 Ctrl+W 而不是 Ctrl+F4?

发布于 2024-12-08 13:34:41 字数 577 浏览 1 评论 0原文

需要一些快速帮助,因为我可能缺少在 Qt 中帮助解决这种情况的技巧(我使用最新的 Qt 4)。

在我的 QMdiArea 中,我创建了一些子窗口。所有子窗口都附加了默认的系统菜单(最小化、最大化、留在顶部、关闭...... - 右键单击​​子窗口图标时可以看到)。

为了与我在应用程序主菜单中定义的内容保持一致(ctrl+w 对于关闭窗口可见),我不能接受与“关闭”相关的显示快捷方式是 Ctrl+F4。必须是 Ctrl+w。我尝试过不同的方法,包括为应用程序设置全局快捷方式(使用 setShortcutContext)...但没有运气。

Close 默认有两个快捷键:Ctrl+wCtrl+F4。我希望两者都继续工作,但应该始终显示 Ctrl+w

目前,我看到的唯一解决方案是替换系统菜单(QMenu)...但这对于如此简单的任务来说似乎有很多代码!

some quick help is needed, as I am probably missing the trick that would help with this situation in Qt (I use the latest Qt 4).

Within my QMdiArea, I create a few subwindows. All the subwindows have the default system menu attached (minimize, maximize, stay on top, close, ... - that's seen when right-clicking on the subwindow icon).

To be consistent with what I have defined in the main menu of my application (ctrl+w visible for closing windows), I cannot accept that the displayed shortcut related to Close is Ctrl+F4 in the subwindows' system menu. It has to be Ctrl+w. I have tried different things, including setting the shortcut global for the application (with setShortcutContext)... but no luck.

Close has by default two shortcuts: Ctrl+w and Ctrl+F4. I want both to keep working, but it's Ctrl+w which should always be displayed.

For now, the only solution I see is to replace the system menu (a QMenu)... but that seem to be a lot of code for such a simple task!

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

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

发布评论

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

评论(1

零時差 2024-12-15 13:34:41

您可以尝试这样设置:

ui->mdiArea->subWindowList().at(index)->systemMenu()->actions().last()->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));

请将“index”替换为合适的索引。
这对我有用。 (更新:这不起作用,CTRL+W 已经是关闭操作的指定快捷方式)

更新:

关闭操作有两个指定的快捷方式,第一个显示在菜单。如果交换顺序,则会显示 CTRL+W

QList<QKeySequence> closeShorcuts = ui->mdiArea->subWindowList().at(0)->systemMenu()->actions().last()->shortcuts();
closeShorcuts.swap(0, 1);
ui->mdiArea->subWindowList().at(0)->systemMenu()->actions().last()->setShortcuts(closeShorcuts);

请注意,此源代码中没有平台检查;如果你在 MAC 上运行它,你会得到相反的结果,并且在 X11 中它将超出列表范围(因为只有一个快捷键:CTRL+W) 。

You can try setting it this way:

ui->mdiArea->subWindowList().at(index)->systemMenu()->actions().last()->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));

Please replace "index" with suitable index.
It worked for me. (UPDATE: this does not work, CTRL+W is already an assigned shortcut for close action)

UPDATE:

Close action has two assigned shortcuts, and the first one is displayed at menu. If you swap the order, you'll get CTRL+W displayed.

QList<QKeySequence> closeShorcuts = ui->mdiArea->subWindowList().at(0)->systemMenu()->actions().last()->shortcuts();
closeShorcuts.swap(0, 1);
ui->mdiArea->subWindowList().at(0)->systemMenu()->actions().last()->setShortcuts(closeShorcuts);

Please note that there is no platform check in this source code; if you run this on MAC you'll get the opposite result, and in X11 it will go out of list bounds (because there is only one shortcut: CTRL+W).

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