Close popups when switching applications on Windows.
authorFriedemann Kleint <Friedemann.Kleint@nokia.com>
Fri, 7 Sep 2012 11:42:02 +0000 (13:42 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 11 Sep 2012 10:09:47 +0000 (12:09 +0200)
Bring back code from 4.8 (Note that ALT-TAB is not received
as key event).

Task-number: QTBUG-27146
Change-Id: I6dd2e9c88fdc4c89d26dfaa8ab47deb2be451f25
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@digia.com>
Reviewed-by: Qt Doc Bot <qt_docbot@qt-project.org>
Reviewed-by: Jørgen Lind <jorgen.lind@nokia.com>
src/widgets/kernel/qapplication.cpp
tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp

index 7e672f4..fe95f58 100644 (file)
@@ -2934,6 +2934,16 @@ bool QApplicationPrivate::shouldQuit()
     return QGuiApplicationPrivate::shouldQuit();
 }
 
+static inline void closeAllPopups()
+{
+    // Close all popups: In case some popup refuses to close,
+    // we give up after 1024 attempts (to avoid an infinite loop).
+    int maxiter = 1024;
+    QWidget *popup;
+    while ((popup = QApplication::activePopupWidget()) && maxiter--)
+        popup->close();
+}
+
 /*! \reimp
  */
 bool QApplication::notify(QObject *receiver, QEvent *e)
@@ -3006,9 +3016,13 @@ bool QApplication::notify(QObject *receiver, QEvent *e)
     }
 #endif // QT_NO_GESTURES
 
-    // User input and window activation makes tooltips sleep
     switch (e->type()) {
-    case QEvent::Wheel:
+    case QEvent::ApplicationDeactivate:
+        // Close all popups (triggers when switching applications
+        // by pressing ALT-TAB on Windows, which is not receive as key event.
+        closeAllPopups();
+        break;
+    case QEvent::Wheel: // User input and window activation makes tooltips sleep
     case QEvent::ActivationChange:
     case QEvent::KeyPress:
     case QEvent::KeyRelease:
index d41bd85..0d3f06f 100644 (file)
@@ -5305,6 +5305,7 @@ void tst_QWidget::setCursor()
         QWidget child(&window);
 
         window.show();
+        QVERIFY(QTest::qWaitForWindowExposed(&window));
         window.setCursor(Qt::WaitCursor);
         QVERIFY(window.testAttribute(Qt::WA_SetCursor));
         QVERIFY(!child.testAttribute(Qt::WA_SetCursor));
@@ -5375,6 +5376,15 @@ void tst_QWidget::setCursor()
 void tst_QWidget::setToolTip()
 {
     QWidget widget;
+    widget.resize(200, 200);
+    // Showing the widget is not required for the tooltip event count test
+    // to work. It should just prevent the application from becoming inactive
+    // which would cause it to close all popups, interfering with the test
+    // in the loop below.
+    widget.setObjectName(QLatin1String("tst_qwidget setToolTip"));
+    widget.setWindowTitle(widget.objectName());
+    widget.show();
+    QVERIFY(QTest::qWaitForWindowExposed(&widget));
     EventSpy spy(&widget, QEvent::ToolTipChange);
     QCOMPARE(spy.count(), 0);
 
@@ -5390,6 +5400,8 @@ void tst_QWidget::setToolTip()
 #ifndef Q_OS_WINCE_WM
     for (int pass = 0; pass < 2; ++pass) {
         QScopedPointer<QWidget> popup(new QWidget(0, Qt::Popup));
+        popup->setObjectName(QString::fromLatin1("tst_qwidget setToolTip #%1").arg(pass));
+        popup->setWindowTitle(popup->objectName());
         popup->resize(150, 50);
         QFrame *frame = new QFrame(popup.data());
         frame->setGeometry(0, 0, 50, 50);