title: How do I detect application Level Focus-In in Qt 4.4.1?
body: <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>