I included #include <QFocusEvent>
into my code and implemented focusOutEvent
as follows:
void MyWidget::focusOutEvent(QFocusEvent *e)
{
if(e->type() == QEvent::FocusOut)
{
fprintf(stderr, "hello");
}
}
Widget descriptor has the following extra lines:
installEventFilter(this);
setFocusPolicy(Qt::WheelFocus);
I also have definition on header file as follows:
virtual void focusOutEvent(QFocusEvent * event);
The problem is, whatever I do (tab or clicking somewhere else) did not call this focus event. How can I solve this?
What did the trick for me was to force the focus on the dialog. i.e.:
QDialog *dialog = new QDialog();
...
dialog->show();
dialog->raise(); // to make sure it's shown on top
dialog->setFocus();
Originally I had a dialog->activateWindow()
that's supposed to set the focus to the dialog just the same, but setFocus()
seems to get the job better done.
Then my re-implemented focusOutEvent()
got called when I clicked anywhere outside the dialog.
Note: This was tested on Fedora 25 (KDE Spin) with Qt 5.7.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With