How to detect focus out event on a widget if it contains child widgets
-
Hi all,
If I have a widget (let's called it A) which has many level of childrens, with each level, has more than one child; what will be the best way to detect focus out event from widget A. i.e, when I click anywhere outside of widget A. Also note that, widget A can have many other siblings.
I know that one of the way I can use is to subscribe to QApplication::focusChanged(QWidget* prev, QWidget* next). However, that would be expensive since for every focus change, the slot connected to this signal will be notified, and for every function call I will have to check the ancestor of prev or prev is not equal to widget A.
Thanks in advance.
-
Did you look at reimplementing focusin and focusout event handlers of qwidget ?
-
@dheerendra widget A will not even get the focus in or out if the child widget already consumed it (in the case where child widget has focus policy other than NoFocus)
-
Would like child and parent both process focus event or only parent ?
-
@Sivan
I'm not sure of the order events happen/who they get (re-)directed to when, but I would be looking at yourQWidget
s' (or maybe even higher up the tree, like the parent window's) event filter, https://doc.qt.io/qt-5/qobject.html#eventFilter ; maybe have a look at https://stackoverflow.com/a/1990462. That is a centralized place where you could implement your logic without having to act onfocusChanged
of each child widget, provided you receive some event there.... -
@JonB the child widget's would still be getting the focusIn or focusOut even if the eventFilter is installed at the parent level. What that means is, I will have to install eventFilter for all the children/grandchildren etc of the parent widget, and in the eventFilter function, I need to check if the obj is a child of widget A