<p>I need to determine when my Qt 4.4.1 application receives focus.</p>

<p>I have come up with 2 possible solutions, but they both don’t work exactly as I would like.</p>

<p>In the first possible solution, I connect the focusChanged() signal from qApp to a SLOT.  In the slot I check the ‘old’ pointer.  If it ‘0’, then I know we’ve switched to this application, and I do what I want.  This seems to be the most reliable method of getting the application to detect focus in of the two solutions presented here, but suffers from the problem described below. </p>

<p>In the second possible solution, I overrode the ‘focusInEvent()’ routine, and do what I want if the reason is ‘ActiveWindowFocusReason’.</p>

<p>In both of these solutions, the code is executed at times when I don’t want it to be.</p>

<p>For example, I have this code that overrides the focusInEvent() routine:</p>

<pre><code>void
ApplicationWindow::focusInEvent( QFocusEvent* p_event )
{

  Qt::FocusReason reason = p_event-&gt;reason();

  if( reason == Qt::ActiveWindowFocusReason &amp;&amp; 
      hasNewUpstreamData() )
  {
    switch( QMessageBox::warning( this, "New Upstream Data Found!",
                                  "New upstream data exists!\n"
                                  "Do you want to refresh this simulation?",
                                  "&amp;Yes", "&amp;No", 0, 0, 1 ) )
    { 
    case 0: // Yes
      refreshSimulation();
      break;
    case 1: // No
      break;
    }
  }
}
</code></pre>

<p>When this gets executed, the QMessageBox dialog appears.  However, when the dialog is dismissed by pressing either ‘yes’ or ‘no’, this function immediately gets called again because I suppose the focus changed back to the application window at that point with the ActiveWindowFocusReason.  Obviously I don’t want this to happen.</p>

<p>Likewise, if the user is using the application opening &amp; closing dialogs and windows etc, I don’t want this routine to activate. NOTE: I’m not sure of the circumstances when this routine is activated though since I’ve tried a bit, and it doesn’t happen for all windows &amp; dialogs, though it does happen at least for the one shown in the sample code.</p>

<p>I only want it to activate if the application is focussed on from outside of this application, not when the main window is focussed in from other dialog windows.</p>

<p>Is this possible?  How can this be done?</p>

<p>Thanks for any information, since this is very important for our application to do.</p>

<p>Raymond.</p>

[{'answer_id': 62820, 'author': 'David Dibben', 'author_id': 5022, 'author_profile': 'https://Stackoverflow.com/users/5022', 'pm_score': 0, 'selected': False, 'text': '<p>Looking at the Qt docs it seems that focus events are created each time a widget gets the focus, so the sample code you posted won\'t work for the reasons you stated. </p>\n\n<p>I am guessing that QApplication::focusedChanged does not work the way you want because some widgets don\'t accept keyboard events so also return null as the "old" widget even when changing focus within the same app.</p>\n\n<p>I am wondering whether you can do anything with QApplication::activeWindow()</p>\n\n<blockquote>\n  <p>Returns the application top-level window that has the keyboard input focus, or 0 if no application window has the focus. Note that there might be an activeWindow() even if there is no focusWidget(), for example if no widget in that window accepts key events.</p>\n</blockquote>\n'}, {'answer_id': 173692, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>When your dialog is open, keyboard events don't go to your main window. After the dialog is closed, they do. That's a focus change. If you want to ignore the case where the focus switched from another window in your application, then you need to know when any window in your application has the focus. Make a variable and add a little more logic to your function. This will take some care, as the dialog will lose focus just before the main window gains focus.</p>\n"}, {'answer_id': 358253, 'author': 'Michael Bishop', 'author_id': 45114, 'author_profile': 'https://Stackoverflow.com/users/45114', 'pm_score': 3, 'selected': False, 'text': '<p>I think you need to track the <a href="http://doc.qt.io/qt-4.8/qevent.html#Type-enum" rel="nofollow noreferrer">QEvent::ApplicationActivate</a> event.</p>\n\n<p>You can put an <a href="http://doc.qt.io/qt-4.8/qobject.html#eventFilter" rel="nofollow noreferrer">event filter</a> on your QApplication instance and then look for it.</p>\n\n<pre><code>bool\nApplicationWindow::eventFilter( QObject * watched, QEvent * event )\n{\n    if ( watched != qApp )\n        goto finished;\n\n    if ( event-&gt;type() != QEvent::ApplicationActivate )\n        goto finished;\n\n    // Invariant: we are now looking at an application activate event for\n    //            the application object\n    if ( !hasNewUpstreamData() )\n        goto finished;\n\n    QMessageBox::StandardButton response =\n            QMessageBox::warning( this, "New Upstream Data Found!",\n                                  "New upstream data exists!\\n"\n                                  "Do you want to refresh this simulation?",\n                                  QMessageBox::Yes | QMessageBox::No) );\n\n    if ( response == QMessageBox::Yes )\n      refreshSimulation();\n\nfinished:\n    return &lt;The-Superclass-here&gt;::eventFilter( watched, event );\n}\n\nApplicationWindow::ApplicationWindow(...)\n{\n    if (qApp)\n        qApp-&gt;installEventFilter( this );\n    ...\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/62629', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/460958/']