A change in eventFilter function causes weird QGraphicScene behavior

只愿长相守 提交于 2019-12-24 15:00:06

问题


I have a Qt program with a QGraphicScene inside a QGraphicsView on top of QMainWindow. The events are handled by the QMainWindow using an eventFilter function. The function body looks similar to this code:

bool Window::eventFilter(QObject *, QEvent *event) {
  QEvent::Type type = event->type();

  if (type == QEvent::KeyPress) {
    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);

    switch(keyEvent->key()) {
    case Qt::Key_A:
    case Qt::Key_B:
    case Qt::Key_C:
    case Qt::Key_D:
      // call a function that uses the current mouse position on the graphics scene
      break;
    default:
      QLocale loc = QApplication::keyboardInputLocale();
      if(loc.language() != QLocale::English) {
        QString message = "A non-English key was pressed";
        showMessage(message, QMessageBox::Warning);
      }
    }

    return true;
  }

  return false;
}

I recently added the "default" part and since then the coordinates that are used in the A,B,C,D cases are totally wrong. In addition, if I add a simple cout print anywhere in the function the bug disappears and the correct mouse coordinates are used.

What can possibly cause this?

来源:https://stackoverflow.com/questions/12297478/a-change-in-eventfilter-function-causes-weird-qgraphicscene-behavior

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!