Aleksey Kontsevich
Aleksey Kontsevich

Reputation: 4991

Escape in QDialog works only in modal mode?

Esc does not work for my dialog in non-modal mode, event going to underlying window. Only adding

setWindowModality(Qt::WindowModal);

to the dialog on creation makes the Esc key work and calls the reject() slot. Is it normal or possible to Esc in/from non-modal dialog?

Upvotes: 0

Views: 704

Answers (2)

Aleksey Kontsevich
Aleksey Kontsevich

Reputation: 4991

Changed

dialog->show();

call to

// Shows the dialog as a modal dialog, blocking until the user closes it.
dialog->exec();

Works fine for now. Seems the only way to catch event in the dialog, non-modal does not allow this or use key event filter as mentioned in another answer.

Upvotes: 0

Andéol Evain
Andéol Evain

Reputation: 517

Maybe with an eventfilter, if you make your custom QDialog? If you call

qApp->installEventFilter(yourQDialog);

when creating it, you can define the function

bool eventFilter(QObject *watched, QEvent *event);

to catch the "esc" you are looking for. More on event filters here: http://doc.qt.io/qt-5/qobject.html#installEventFilter.

Upvotes: 1

Related Questions