背景
- 单个按键的值检测:
- 组合按键的检测: Ctrl+x
问题
自定义的Widget没有响应。 查看QWidget的keyPressEvent 说明:
- 在构造函数中使用setFocusPolicy( ), 才能接收到按键事件
- 如果重写了该接口,有一点非常重要:如果不需要对某个按键进行处理,调用基类的处理函数。
keypressEvent
void ViewWidget::ViewWidget()
{
setFocusedPolicy(Qt::StrongFocus);
...
}
void ViewWidget::keyPressEvent(QKeyEvent *event)
{
if(event->modifiers() == Qt::ControlModifier)
{
switch(event->key())
{
...
}
}
else
{
switch(event->key())
{
...
}
}
}