方案:参考Qt帮助文档,重写KeyPressEvent().
在qnamespace.h中,枚举了KeyBoardModifier
enum KeyboardModifier {
NoModifier = 0x00000000,
ShiftModifier = 0x02000000,
ControlModifier = 0x04000000,
AltModifier = 0x08000000,
MetaModifier = 0x10000000,
KeypadModifier = 0x20000000,
GroupSwitchModifier = 0x40000000,
// Do not extend the mask to include 0x01000000
KeyboardModifierMask = 0xfe000000
};
Q_DECLARE_FLAGS(KeyboardModifiers, KeyboardModifier)
Q_DECLARE_OPERATORS_FOR_FLAGS(KeyboardModifiers)
举四个组合键的实例:
if(ui->cb_bossKey->currentIndex() == 0)
{
if (ev->modifiers() == (Qt::AltModifier | Qt::ControlModifier) && ev->key() == Qt::Key_0)
{
//相关功能实现
}
}
if(ui->cb_bossKey->currentIndex() == 1)
{
if(ev->modifiers() == (Qt::AltModifier | Qt::ControlModifier) && ev->key() == Qt::Key_Z)
{
//相关功能实现
}
}
if(ui->cb_bossKey->currentIndex() == 2)
{
if(ev->modifiers() == (Qt::ShiftModifier | Qt::ControlModifier) && ev->key() == Qt::Key_0)
{
//相关功能实现
}
}
if(ui->cb_bossKey->currentIndex() == 3)
{
if(ev->modifiers() == (Qt::ShiftModifier | Qt::ControlModifier) && ev->key() == Qt::Key_Z)
{
//相关功能实现
}
}else {
}