0

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?

Murphy
  • 3,827
  • 4
  • 21
  • 35
Aleksey Kontsevich
  • 4,671
  • 4
  • 46
  • 101

2 Answers2

1

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.

marco.m
  • 4,573
  • 2
  • 26
  • 41
Andéol Evain
  • 507
  • 2
  • 12
  • 1
    "maybe" not a good answer here. Try Your answer first. Why I should make such strange workaround with event filter here? Doc says nothing about. – Aleksey Kontsevich Mar 02 '18 at 17:54
0

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.

Aleksey Kontsevich
  • 4,671
  • 4
  • 46
  • 101