问题描述
我希望QAction由多个键盘快捷键触发.根据文档,应该通过将QKeySequences列表传递给 QAction来实现:: setShortcuts(),但实际上似乎只使用列表中的第一个键序列.
I want a QAction to be triggered by multiple keyboard shortcuts. According to the documentation, this should be possible by passing a list of QKeySequences to QAction::setShortcuts() , but in practice it seems that only the first key sequence in the list is used.
我创建了一个简短的示例来演示该问题.在这里,我试图同时包含 + 和 + (这是 + 和Mac上的 + )触发我的操作".以这种形式,只有 + 会触发操作.如果交换了两个快捷方式,以便首先使用 + ,则它将触发操作,并触发 + 不会.最后,如果所有快捷方式列表中的内容都被注释掉,并且QKeySequence :: Cut所在的行未注释,则即使文档说,它既映射到 + K 和 + .代码:
I've created a short example that demonstrates the problem. Here I'm trying to have both + and + (which are + and + on the Mac) trigger "My Action". In this form, only + will trigger the action. If the two shortcuts are swapped so that + is first, then it will trigger the action and + will not. Finally, if all of the shortcut list stuff is commented out and the line with QKeySequence::Cut is uncommented, only + will work even though the documentation says that it maps to both + and + on the Mac. The code:
#include "mainwindow.h"
#include <QMenuBar>
#include <QAction>
MainWindow::MainWindow() {
QMenu* menu = new QMenu("Menu");
menuBar()->addMenu(menu);
QAction* action = new QAction("My Action", this);
// Create a list with two shortcuts
QList<QKeySequence> shortcuts;
shortcuts.append(QKeySequence(Qt::META + Qt::Key_K));
shortcuts.append(QKeySequence(Qt::CTRL + Qt::Key_X));
action->setShortcuts(shortcuts);
// Alternatively, use one of the QKeySequence::StandardKey enums
//action->setShortcuts(QKeySequence::Cut);
connect(action, SIGNAL(triggered()), this, SLOT(doIt()));
menu->addAction(action);
}
// slot
void MainWindow::doIt() { qDebug("MainWindow::doIt()"); }
如果有关系,我正在使用Mac OS X 10.9.2,并且已经在Qt 5.2.1和Qt 5.3.0中进行了尝试.
If it matters, I'm using Mac OS X 10.9.2, and I've tried this with Qt 5.2.1 and Qt 5.3.0.
我包含了头文件和main.cpp,以防任何人只想复制并粘贴来运行示例,但是其中没有有趣或不标准的地方.头文件:
I'm including the header file and main.cpp in case anyone wants to just copy-and-paste to run the example, but there is nothing interesting or non-standard in them. Header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
public slots:
void doIt();
};
#endif // MAINWINDOW_H
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
推荐答案
OS X不支持菜单项的多个键盘快捷键. QAction文档指出调用此函数的结果将取决于当前运行的平台".
Multiple keyboard shortcuts for a menu item are not supported on OS X. The QAction documentation notes "The result of calling this function will depend on the currently running platform".
看来,根本原因是OS X 保持稳定菜单->字典中的快捷键映射,其中菜单文本为键,值为单个键原子(不是任何种类的集合),因此每个菜单项只能有一个活动的快捷键时间.
It looks like the underlying cause is that OS X keeps the menu -> key shortcut mappings in a dictionary with the menu text as the key and the value as a single key atom (not any sort of collection), so there can be only one active key shortcut per menu item at a time.
此处用户发现,即使在本机应用程序中为同一菜单项手动添加多个键盘快捷键也无法正常工作-仅使用了第一个.
Here a user found that even manually adding multiple keyboard shortcuts for the same menu item in a native application did not work - only the first was used.
这篇关于QAction :: setShortcuts()仅响应列表中的第一个快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!