如何在 QMenu 中为 QAction 指定助记符(与符号快捷方式)?

发布于 2025-01-01 11:11:28 字数 640 浏览 0 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(2

梦晓ヶ微光ヅ倾城 2025-01-08 11:11:28

可以创建仅在菜单上下文中有效的快捷方式

QAction * recentFileAction = new QAction( tr("A recent file"), this );
recentFileAction->setShortcut( QKeySequence( tr("Ctrl+1") ) );

QMenu * tools = menuBar()->addMenu( tr("&Tools") );

// Add a shortcut valid only when the tools menu has focus
QShortcut * recentFileShortcut = new QShortcut( QKeySequence( tr("1") ), tools );
recentFileShortcut->setContext( Qt::WidgetShortcut );

connect( recentFileShortcut, SIGNAL(activated()),
         recentFileAction,   SLOT(trigger()));

您 可能需要将菜单的焦点策略设置为Qt::StrongFocus,以便它接受键盘输入。

You can create a shortcut valid only in the context of the menu:

QAction * recentFileAction = new QAction( tr("A recent file"), this );
recentFileAction->setShortcut( QKeySequence( tr("Ctrl+1") ) );

QMenu * tools = menuBar()->addMenu( tr("&Tools") );

// Add a shortcut valid only when the tools menu has focus
QShortcut * recentFileShortcut = new QShortcut( QKeySequence( tr("1") ), tools );
recentFileShortcut->setContext( Qt::WidgetShortcut );

connect( recentFileShortcut, SIGNAL(activated()),
         recentFileAction,   SLOT(trigger()));

You might need to set the menu's focus policy to Qt::StrongFocus so that it accepts keyboard inputs.

我的黑色迷你裙 2025-01-08 11:11:28

后记:

  • 分别使用 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 使用快捷方式填充右列(我在提出问题之前所拥有的,相当标准):

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

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

Post-Notes:

  • Tested and confirmed working on linux / windows, using Qt-4.6.3-rh5-x86_64 and Qt-4.6.4-win32 respectively.
  • Tested and reported not working on Mac OS X by Kamil Klimek.

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:

// Existing: QMenu* fileMenu_
QAction* action = new QAction("Recent file name", fileMenu_)
action->setShortcut(QKeySequence(QString("CTRL+").append(QString::number(1))));
fileMenu_->addAction(action);

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.

| Recent file name      Ctrl+1 |

This can be customized easily by use of a an escaped tab. Such that using:

QAction* action = new QAction("Some text\tOther text", fileMenu_)
action->setShortcut(QKeySequence(QString("CTRL+").append(QString::number(1))));

Results in

| Some text       Other text |

While still retaining the default Ctrl+1 shortcut when out of focus. This leads to the solution:

QAction* action = new QAction(QString("Recent file name\tCtrl+&%1").arg(i)), fileMenu_)
action->setShortcut(QKeySequence(QString("CTRL+").append(QString::number(i))));

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.

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