Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

keyPressEvent subclass works only in debug



  • I'm not sure what I'm doing wrong here.
    In my QDialog form I want to customize the pressing of some keys, like esc.
    So I wrote:

    DialogComponents.h

    protected:
        void keyPressEvent(QKeyEvent *e);
    

    DialogComponents.cpp

    void DialogComponents::keyPressEvent(QKeyEvent *e)
    {
        qDebug() << e->key();
        if (e->key() == Qt::Key_Escape) emit swapCurrentStack();
        else QDialog::keyPressEvent(e);
    }
    

    if I run the debugger (run a debug build is not enough) it works like expected:

    1. it prints the key() value
    2. if I press key is esc it emits the signal and executes the connected slot
    3. if I press another key, i.e. enter the default behavior is unchanged

    but if I run the application (even built as debug) without attach the debugger, it just does nothing: no qDebug() and no effect at all. Of course all other qDebug() I have work fine.

    The mistake is in the code above?


  • Lifetime Qt Champion

    @Mark81 said in keyPressEvent subclass works only in debug:

    protected:
    void keyPressEvent(QKeyEvent *e);

    You can add override to make sure you really re-implement the base implementation:

    void keyPressEvent(QKeyEvent *e) override;

    Otherwise I don't see what could be wrong with your code.

    Regards



  • @aha_1980 well, the override keyword does nothing, apparently.
    But I found a more specific criteria for the weird behavior.

    Now I can say the wrong behavior is not due to the debugger.
    Sorry for the misleading title.

    When I run the application the keyPressEvent is not fired until I minimize and restore the window.
    I don't know why.

    Some other stuff that might be related.
    I also implemented the showEvent function to update the QTableView model when the dialog is shown:

    void DialogComponents::showEvent(QShowEvent *event)
    {
        Q_UNUSED(event)
        qDebug() << "show event fired";
        ui->table->clearSelection();
        _model->select();
    }
    

    but this fires anyway, even when I open it the first time, of course.


  • Lifetime Qt Champion

    Hi,

    Are you sure your dialog has the keyboard focus and not one of the widget inside of it ?



  • @SGaist said in keyPressEvent subclass works only in debug:

    Are you sure your dialog has the keyboard focus and not one of the widget inside of it ?

    It should - at least I assume that - because if I remove the override function, when I press esc the QDialog closes (actually, rejects) under the same conditions.


  • Moderators

    What version of Qt are we talking about, and what OS?



  • Qt 5.12.2, Windows 7


  • Moderators

    @Mark81 I would suggest to update to at least the latest version of 5.12, as it is a LTS version.

    Also you know, that support for win 7 will be close to non existent come January ?



  • My guess is the keyboard focus. Try setFocus() on your dialog when it opens.



  • @qwasder85 said in keyPressEvent subclass works only in debug:

    My guess is the keyboard focus. Try setFocus() on your dialog when it opens.

    Yes, it's definitely related to the focus. It's strange that the default event works in any case.


Log in to reply