如何在 QMenu 中为 QAction 指定助记符(与符号快捷方式)?
我在 QMenu 中使用 QActions,典型的
| &New file Ctrl+N |
| &Open file Ctrl+O |
在菜单打开时会获得一个很好的上下文快捷方式,即 N(表示新文件)和 O(表示打开文件)。
我想做一些类似的事情来列出最近的文件,即:
| [A recent file] Ctrl+1 |
| [Another recent file] Ctrl+2 |
... etc
将助记符/上下文快捷方式设置为相应的 1 和 2 会很好,不必在文本中包含此数字 field:
| &1. [A recent file] Ctrl+1 |
| &2. [Another recent file] Ctrl+2 |
如果有人知道如何做到这一点,或者可以指出我寻找答案的方向,我会很高兴。我浏览了一些文档,但没有找到太多关于使用 & 符号以及设置 QActions 助记符快捷方式的等效方法的提及。
谢谢。
PS:Qt-4.7.4-rh6-x86_64,C++
I'm using QActions in a QMenu, the typical
| &New file Ctrl+N |
| &Open file Ctrl+O |
Which gets a nice context shortcut of simply N (for New File) and O (for Open File) while the menu is open.
I'd like to do something similar for listing recent files, i.e.:
| [A recent file] Ctrl+1 |
| [Another recent file] Ctrl+2 |
... etc
It would be nice to set the mnemonic/context shortcut to the respective 1, and 2, without having to include this number in the text field:
| &1. [A recent file] Ctrl+1 |
| &2. [Another recent file] Ctrl+2 |
If anyone knows how to do this, or can point me in the direction of finding out, I'd be happy. I've looked through some of the documentation, and I can't find much mention of using the ampersand and equivalent ways of setting the mnemonic shortcut for QActions.
Thanks.
Ps: Qt-4.7.4-rh6-x86_64, C++
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以创建仅在菜单上下文中有效的快捷方式:
您 可能需要将菜单的焦点策略设置为
Qt::StrongFocus
,以便它接受键盘输入。You can create a shortcut valid only in the context of the menu:
You might need to set the menu's focus policy to
Qt::StrongFocus
so that it accepts keyboard inputs.后记:
我不完全确定这是否是 Qt 预期功能的一部分,或者只是一个 hack。我找不到任何文档,暗示了后者,但它工作得很好暗示了前者。你是法官,让我/我们知道。
通常的用法是:
现在,显然,Qt 将文件菜单填充为具有两列的表格。默认行为是在左列上显示标签(名称),在右列上显示格式化的快捷键。
这可以通过使用转义选项卡轻松定制。这样使用:
导致
在失焦时仍然保留默认的 Ctrl+1 快捷键。这就得出了解决方案:
其中变量
i
表示最近文件的索引。这正是我想要的,并且还在数字下方显示了一个下划线,这很好地表明了助记符快捷方式。更新
为了演示最终结果,我添加了一些图像,以防出现任何混淆。
允许 Qt 使用快捷方式填充右列(我在提出问题之前所拥有的,相当标准):
手动填充右列后,并添加助记符:
除了表示助记符的下划线外,这对我来说看起来是相同的。
Post-Notes:
I'm not entirely sure if this is part of Qt intended functionality, or simply a hack. That I can't find any documentation, alludes to the latter, but that it works so nicely suggests the former. You be the judge, and let me/us know.
The usual usage was:
Now, apparently, Qt populates the file menu as a table with two columns. The default behavior is having the label (name), on the left column, and the formatted shortcut keys on the right column.
This can be customized easily by use of a an escaped tab. Such that using:
Results in
While still retaining the default Ctrl+1 shortcut when out of focus. This leads to the solution:
Where the variable
i
denotes the index of the recent file. This creates exactly what I had in mind, and also shows a underscore under the number, which indicates the mnemonic shortcut nicely.Update
Just to demonstrate the end result, I've added some images in case there is any confusion.
Allowing Qt to populate the right column with the shortcut (what I had before asking the question, pretty standard):
After manually populating the right column, and also adding the mnemonic:
Which to me look identical, aside from the underline denoting the mnemonic.