如何在Qt中使用两个键修饰符设置3键序列快捷方式? [英] How to set 3-key sequence shortcut with two key modifiers in Qt?
问题描述
我一直试图将快捷方式设置为 Ctrl + Shift + C .
I have been trying to set a shortcut as 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 时,什么都不会触发.
They didn't work. Nothing is triggered when I press Ctrl + Shift +C.
是否不可能在Qt中使用两个修饰符设置快捷方式?
Is it impossible to set a shortcut with two modifiers in Qt?
推荐答案
我写了一个最小的完整示例.在我看来,这对您的描述很有用.可能是,我添加了一些您不支持的内容. (因此,首选最小,完整,可验证的样本".)
I wrote a minimal, complete sample. It worked in my case how you described it. May be, I added something which you didn't on your side. (That's why "minimal, complete, verifiable samples" are preferred.)
// 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()
.如果我将其注释掉,则它在我的程序中也不再起作用.
I suspect that you didn't call QWidget::addAction()
. If I comment it out it does not work anymore in my program also.
在Windows 10(64位)上与VS2013和Qt 5.6一起编译:
Compiled with VS2013 and Qt 5.6 on Windows 10 (64 bit):
此快照是在按Ctrl + Shift + Q后创建的.
This snapshot has been made after pressing Ctrl+Shift+Q.
注意:
此后,我意识到实际的问题是关于"Ctrl + Shift + C"的.可以肯定的是,我检查了一下.上面的示例代码也可以与"Ctrl + Shift + C"一起使用.
I realized afterwards that the actual question was about "Ctrl+Shift+C". To be sure, I checked it. The above sample code works with "Ctrl+Shift+C" as well.
这篇关于如何在Qt中使用两个键修饰符设置3键序列快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!