c++ - 如何在 Qt 中使用两个键修饰符设置 3 键序列快捷方式?

标签 c++ qt c++11 qt5 keyboard-shortcuts

我一直在尝试将快捷方式设置为 Ctrl + Shift +C

我试过以下方法:

QAction *generalControlAction = new QAction(this);
generalControlAction->setShortcut(QKeySequence("Ctrl+Shift+c"));
connect(generalControlAction, &QAction::triggered, this, &iBexWorkstation::onGeneralConfiguration);

QShortcut *generalControlShortcut = new QShortcut(QKeySequence("Ctrl+Shift+C"), this);
connect(generalControlShortcut, &QShortcut::activated, this, &iBexWorkstation::onGeneralConfiguration);

他们没有工作。当我按下 Ctrl + Shift +C 时没有触发任何东西。

在Qt中不能用两个修饰符设置快捷方式吗?

最佳答案

我写了一个最小的、完整的示例。它在我的案例中是如何描述的。可能是,我添加了一些您没有添加的内容。 (这就是为什么首选“最少的、完整的、可验证的样本”。)

// standard C++ header:
#include <iostream>

// Qt header:
#include <QAction>
#include <QApplication>
#include <QLabel>
#include <QMainWindow>

using namespace std;

int main(int argc, char **argv)
{
  cout << QT_VERSION_STR << endl;
  // main application
#undef qApp // undef macro qApp out of the way
  QApplication qApp(argc, argv);
  // the short cut
  const char *shortCut = "Ctrl+Shift+Q";
  // setup GUI
  QMainWindow qWin;
  QAction qCmdCtrlShiftQ(&qWin);
  qCmdCtrlShiftQ.setShortcut(QKeySequence(shortCut));
  qWin.addAction(&qCmdCtrlShiftQ); // DON'T FORGET THIS.
  QLabel qLbl(
    QString::fromLatin1("Please, press ")
    + QString::fromLatin1(shortCut));
  qLbl.setAlignment(Qt::AlignCenter);
  qWin.setCentralWidget(&qLbl);
  qWin.show();
  // add signal handlers
  QObject::connect(&qCmdCtrlShiftQ, &QAction::triggered,
    [&qLbl, shortCut](bool) {
    qLbl.setText(
      QString::fromLatin1(shortCut)
      + QString::fromLatin1(" pressed."));
  });
  // run application
  return qApp.exec();
}

我怀疑您没有调用QWidget::addAction()。如果我将其注释掉,它在我的程序中也不再起作用。

在 Windows 10(64 位)上使用 VS2013 和 Qt 5.6 编译:

Snapshot of testQShortCut.exe (after pressing Ctrl+Shift+Q)

此快照是在按 Ctrl+Shift+Q 后制作的。

注意:

后来我意识到实际问题是关于“Ctrl+Shift+C”的。可以肯定的是,我检查了它。上面的示例代码也适用于“Ctrl+Shift+C”。

关于c++ - 如何在 Qt 中使用两个键修饰符设置 3 键序列快捷方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42876347/

相关文章:

c++11 - fatal error -没有此类文件或目录

c++ - 如何确保我的 `ifstream` 文件对象指向的文件内容已更新?

c++ - 我可以使用 sendto() 或 sendmsg() 将数据包推送到服务器的 recvmmsg() 吗?

c++ - 聚合初始化,clang 要我破坏我的代码?

c++ - Windows 中的启动画面

c++ - 服务器端识别客户端的方法

c++ - QDataStream operator << 抽象类的重载

javascript - QML 中 default 关键字的用途是什么?

c++ - Qt/C++ - 将字符串时间戳转换为 uint

c++ - 自动返回类型不推导引用