我一直在尝试将快捷方式设置为 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 编译:
此快照是在按 Ctrl+Shift+Q 后制作的。
注意:
后来我意识到实际问题是关于“Ctrl+Shift+C”的。可以肯定的是,我检查了它。上面的示例代码也适用于“Ctrl+Shift+C”。
关于c++ - 如何在 Qt 中使用两个键修饰符设置 3 键序列快捷方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42876347/