1

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?

4

0 回答 0