后注:
- 分别使用 Qt-4.6.3-rh5-x86_64 和 Qt-4.6.4-win32 测试并确认在 linux / windows 上工作。
- Kamil Klimek 测试并报告无法在 Mac OS X 上运行。
我不完全确定这是否是 Qt 预期功能的一部分,或者只是一个 hack。我找不到任何文档,暗示后者,但它工作得很好,暗示了前者。你是法官,让我/我们知道。
通常的用法是:
// Existing: QMenu* fileMenu_
QAction* action = new QAction("Recent file name", fileMenu_)
action->setShortcut(QKeySequence(QString("CTRL+").append(QString::number(1))));
fileMenu_->addAction(action);
现在,显然,Qt 将文件菜单填充为具有两列的表格。默认行为是在左列有标签(名称),在右列有格式化的快捷键。
| Recent file name Ctrl+1 |
这可以通过使用转义选项卡轻松自定义。这样使用:
QAction* action = new QAction("Some text\tOther text", fileMenu_)
action->setShortcut(QKeySequence(QString("CTRL+").append(QString::number(1))));
结果是
| Some text Other text |
失焦时仍保留默认的 Ctrl+1 快捷键。这导致了解决方案:
QAction* action = new QAction(QString("Recent file name\tCtrl+&%1").arg(i)), fileMenu_)
action->setShortcut(QKeySequence(QString("CTRL+").append(QString::number(i))));
其中变量i
表示最近文件的索引。这完全符合我的想法,并且在数字下方显示了一个下划线,很好地指示了助记符快捷方式。
更新
只是为了演示最终结果,我添加了一些图像以防出现任何混淆。
允许 Qt 使用快捷方式填充右列(我在提出问题之前所拥有的,非常标准):

手动填充右列并添加助记符后:

除了表示助记符的下划线外,这对我来说看起来是一样的。