Important: Please read the Qt Code of Conduct -

keyPressEvent suddenly not firing

  • I have a program I have been working on for over a year. I have multiple forms, one being the Main Window. I made one change to the keyPressEvent method on one of the forms derived from QDialog and now all of a sudden the only form the keypress event works on is the main. All other forms it never fires. The only thing I changed was adding another case to my switch statement to handle a different keypress. Why this would break the keyPress event on all other forms except the main window is beyond me.

    I am grabbing the keyboard in the constructor for the form.

  • Update. When I created an object for one of my forms, I passed in "this" from the Main Window as the parent object. For some reason this breaks the keypress event in the child form.

  • Lifetime Qt Champion

    What key press event handling did you modify ?

  • @SGaist I modified the keyPressEvent handler in the on of my Dialog forms (not the main window). I wanted to add handling when the user presses the 8 key. In my reply I noted that I did also add passing "this" from the main window as the parent object of the dialog when I created the dialog object. When I removed that it started working again.

    The goal is when the users presses the 8 key, to close the Dialog and pass the key press back to the main window.

  • Lifetime Qt Champion

    In that case, shouldn't you rather handle that directly in the main window and use it as a filter on your dialog ?

  • @SGaist I don't understand the question, but here is the situation. The target system has 9 total keys. 1-8 and T. On most screens these keys have the same behavior. However, the 8 key on all screens but the Main Window causes the current screen to close then the Main Window needs to react as if it had focus with the 8 key was pressed.

    I just added code to search through the QWindowList returned by QApplication::topLevelWindows to find a pointer to the Main Window. I then use that pointer in the QApplication::sendEvent to send the 8 key press to it.

    What I am wondering is if it would work the same way if I do not call event->accept() when the 8 key is pressed and just close the current window?

  • Lifetime Qt Champion

    I meant Event Filters.

    So your MainWindow would be doing all the handling of the 8 key itself

  • @SGaist Not really because the current window has to close before the main window handles the 8 key. All the other windows are started using exec() method from the main screen.

    For example I create password screen object (a). I call a.exec() to actually show the screen.

  • Lifetime Qt Champion

    Well, that would be part of the logic of the filter: first step, close the dialog, second handle 8.

    In any case, your idea of not calling accept on the event should result it in being forward to your MainWindow

Log in to reply