Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

focusOutEvent is not called

Tags:

c++

qt

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?

like image 479
www Avatar asked Mar 22 '23 23:03

www


1 Answers

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

like image 147
oranja Avatar answered Apr 05 '23 00:04

oranja