keyPressEvent subclass works only in debug
-
I'm not sure what I'm doing wrong here.
In myQDialog
form I want to customize the pressing of some keys, likeesc
.
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:
- it prints the
key()
value - if I press key is
esc
it emits the signal and executes the connected slot - 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 otherqDebug()
I have work fine.The mistake is in the code above?
- it prints the
-
@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 theshowEvent
function to update theQTableView
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.
-
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
theQDialog
closes (actually, rejects) under the same conditions. -
What version of Qt are we talking about, and what OS?
-
@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.