为什么QEvent :: ShortcutOverride事件发生? [英] Why does QEvent::ShortcutOverride event occur?

查看:1693
本文介绍了为什么QEvent :: ShortcutOverride事件发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个安装了事件过滤器的QMainWindow。
打开并关闭QDialog后,键盘箭头键不响应,因为QMainWindow只接收ShortcutOverride事件而不是KeyPress事件。



当我更改QMainWindow的事件过滤器来处理ShortcutOverride事件我有一个奇怪的行为,因为在我的程序中,每个按键事件都有两个快捷键重写事件(为什么?)。



t工作 - 事件被多次处理:

  bool eventFilter(QObject *,QEvent * event){
if (type == QEvent :: KeyPress || type == QEvent :: ShortcutOverride){
QKeyEvent * keyEvent = static_cast< QKeyEvent *>(event);

switch(keyEvent-> key()){
case Qt :: Key_Up:
case Qt :: Key_Down:
case Qt :: Key_Left:
case Qt :: Key_Right:
//在这里做某事
break;
默认值:
break;
}

返回true;
}

return false;
}

为什么会发生这种情况?那些ShortcutOverride事件来自哪里?



编辑:



事实证明,当QMainwindow失去焦点时当QDialog关闭时,QDialog被打开,永远不会收回。
我在QMainwindow的eventFilter中使用了setFocus(),它似乎在做这个伎俩。

解决方案

正是在打开对话框后没有得到任何键盘事件的原因。您提出的解决方案可能是此问题的最佳解决方案。



对于ShortcutOverride事件,它们来自每个按键,因为任何键或组合键都可以注册为捷径。如果需要,此事件为您提供取消快捷方式处理的选项。当发动机不知道按下的键是否被用作快捷方式时,它必须在尽可能早的时刻进行处理。因此,为所有的按键生成事件以防万一。您可以在这里阅读更多信息: https://wiki.qt.io/ShortcutOverride


I have a QMainWindow with an event filter installed. After I open and close a QDialog the keyboard arrow keys don't respond since QMainWindow receives only ShortcutOverride events instead of KeyPress events.

When I changed the QMainWindow's event filter to handle ShortcutOverride events I got a strange behavior since in my program each key press event is preceded by two shortcut override events (why??).

This doesn't work - events are handled more than once:

bool eventFilter(QObject *, QEvent *event) {
  if(type == QEvent::KeyPress || type == QEvent::ShortcutOverride) {
    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);

    switch(keyEvent->key()) {
    case Qt::Key_Up:
    case Qt::Key_Down:
    case Qt::Key_Left:
    case Qt::Key_Right:
      // Do something here
      break;
    default:
      break;
    }

    return true;
  }

  return false;
}

Why is this happening? Where do those ShortcutOverride events come from?

Edit:

As it turns out, QMainwindow loses focus when the QDialog is opened and never gains it back when the QDialog is closed. I used setFocus() in the eventFilter of QMainwindow and it seems to do the trick.

解决方案

Lost focus is exactly the reason for not getting any keyboard events after a dialog opens up. The solution you suggested is probably the best fix for this issue.

As for ShortcutOverride events, they come for every keypress because any key or combination of keys can be registered as a shortcut. This event gives you an option to cancel shortcut handling, if needed. It has to be processed at the earliest possible moment when the engine doesn't know yet if the pressed key is going to be used as a shortcut or not. Thus the event is generated for all key presses "just in case". You may read more here: https://wiki.qt.io/ShortcutOverride

这篇关于为什么QEvent :: ShortcutOverride事件发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆