title: Close all widgets in Qt except for some
body: <p>I've just started to work on quite sophisticated Qt Application. One thing keeps bothering me. We have an "Automatic logout" feature when after certain time of inactivity most of the widgets are closed and Login prompt is displayed. It's done with this slot:</p>
  
  <pre><code>foreach(QWidget *w, qApp-&gt;topLevelWidgets()) {
     if ( w != loginDialog &amp;&amp;
          w != mainWindow &amp;&amp;
          !w-&gt;objectName().isEmpty() &amp;&amp;
          (w-&gt;isWindow() ) ) {
          w-&gt;close();
     }
  }
  </code></pre>
  
  <p>Basically it iterates over all widgets except LoginDialog and MainWindow. This seemed to work OK until we found out that this doesn't handle Modal dialogs properly. Especially dialogs like <code>QMessageBox::warning</code> etc. The idea now to iterate through modal widgets first and then over normal widgets. What do you think is this the right way to go?</p>
  
  <p>UPD: Sorry, but I have to withdraw my acceptance of the answer. </p>
  
  <p>My problem seems to be described here <a href="https://blogs.kde.org/2009/03/26/how-crash-almost-every-qtkde-application-and-how-fix-it-0" rel="nofollow">https://blogs.kde.org/2009/03/26/how-crash-almost-every-qtkde-application-and-how-fix-it-0</a> . It looks like "Close" request is handled in a local event loop of a MessageBox. I should rephrase my question: is it possible to exit local event loop (i.e. close a MessageBox) and resend a signal to a main event loop?</p>