PDA

View Full Version : get notified when QDockWidget gets/looses focus?



tuli
17th December 2012, 15:57
hi,

I have a QMainWindow hosting several subclassed QDockWidgets. I would now need to detect when one of the QDockWIdgets gets or looses focus.
There doesnt appear to be a signal for that, so i figured i have to catch the event somehow :



class mydockwid : public QDockWidget
{
Q_OBJECT

public:

....

//qt event handlers
virtual void focusInEvent( QFocusEvent* );
};

But the focusInEvent() never gets called. I am also worried that QDockWidget has or will have use for focusInEvent() itself, and i break its functionality.

How to do it properly?

d_stranz
17th December 2012, 17:42
The focusInEvent() occurs only when the widget has a focus policy set. You need to call the QWidget::setFocusPolicy() for the widget. Use Qt::StrongFocus if you want both keyboard and click focus events.

By default, QWidget has Qt::NoFocus, which means they do not receive focus events, so that's why you aren't seeing them.

If you need your dock widget to tell other widgets that it has received (or lost) focus, use the event handlers to emit signals. If you want to be sure that you are not interfering with normal widget behavior, call the base class handler (or simply don't set the event's "accepted" parameter to true).