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

问题描述 投票:6回答:2

我一直在尝试将快捷方式设置为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中设置两个修饰符的快捷方式是不可能的吗?

c++ qt c++11 qt5 keyboard-shortcuts
2个回答
4
投票

我写了一个最小的完整样本。在我的案例中,你如何描述它。可能是,我添加了一些你不喜欢的东西。 (这就是为什么“最小,完整,可验证的样本”是首选。)

// 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”。


2
投票

generalControlAction-> setShortcut(QKeySequence(Ctrl + Shift + C));

只需在代码中更改上述提及语句即可

generalControlAction-> setShortcut((CTRL + SHIFT + C));

这应该工作正常。 “C”应该是以后的资本。

请参考下面给出的链接http://doc.qt.io/qt-5/qkeysequence.html的密钥序列

© www.soinside.com 2019 - 2024. All rights reserved.