Fix setting custom cursor for widgets and windows before showing them
authorMiikka Heikkinen <miikka.heikkinen@digia.com>
Mon, 15 Oct 2012 11:17:01 +0000 (14:17 +0300)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Tue, 16 Oct 2012 13:29:15 +0000 (15:29 +0200)
If custom cursor was set before the window was created, it didn't
actually get set, and in some cases even caused a crash.
Fixed by making sure the cursor is correct when showing widget/window.

Task-number: QTBUG-27535
Change-Id: I3bc946a9c406c96af5b86869a3a54893f8980aba
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: Gatis Paeglis <gatis.paeglis@digia.com>
Reviewed-by: Samuel Rødal <samuel.rodal@digia.com>
src/gui/kernel/qwindow.cpp
src/gui/kernel/qwindow_p.h
src/widgets/kernel/qwidget_qpa.cpp

index 27e1571..39a6603 100644 (file)
@@ -286,6 +286,10 @@ void QWindow::setVisible(bool visible)
             QGuiApplicationPrivate::hideModalWindow(this);
     }
 
+#ifndef QT_NO_CURSOR
+    if (visible)
+        d->applyCursor();
+#endif
     d->platformWindow->setVisible(visible);
 
     if (!visible) {
@@ -1853,11 +1857,10 @@ QCursor QWindow::cursor() const
 void QWindow::setCursor(const QCursor &cursor)
 {
     Q_D(QWindow);
-    if (QPlatformCursor *platformCursor = d->screen->handle()->cursor()) {
-        d->cursor = cursor;
-        QCursor *oc = QGuiApplication::overrideCursor();
-        QCursor c = oc ? *oc : d->cursor;
-        platformCursor->changeCursor(&c, this);
+    d->cursor = cursor;
+    // Only attempt to set cursor and emit signal if there is an actual platform cursor
+    if (d->screen->handle()->cursor()) {
+        d->applyCursor();
         QEvent event(QEvent::CursorChange);
         QGuiApplication::sendEvent(this, &event);
     }
@@ -1868,19 +1871,20 @@ void QWindow::setCursor(const QCursor &cursor)
  */
 void QWindow::unsetCursor()
 {
-    Q_D(QWindow);
-    if (QPlatformCursor *platformCursor = d->screen->handle()->cursor()) {
-        d->cursor = Qt::ArrowCursor;
-        QCursor *oc = QGuiApplication::overrideCursor();
-        if (!oc) {
-            QCursor c = d->cursor;
-            platformCursor->changeCursor(&c, this);
+    setCursor(Qt::ArrowCursor);
+}
+
+void QWindowPrivate::applyCursor()
+{
+    Q_Q(QWindow);
+    if (platformWindow) {
+        if (QPlatformCursor *platformCursor = screen->handle()->cursor()) {
+            QCursor *oc = QGuiApplication::overrideCursor();
+            QCursor c = oc ? *oc : cursor;
+            platformCursor->changeCursor(&c, q);
         }
-        QEvent event(QEvent::CursorChange);
-        QGuiApplication::sendEvent(this, &event);
     }
 }
-
 #endif
 
 QT_END_NAMESPACE
index 57f79f1..159f05d 100644 (file)
@@ -97,6 +97,9 @@ public:
     }
 
     void maybeQuitOnLastWindowClosed();
+#ifndef QT_NO_CURSOR
+    void applyCursor();
+#endif
 
     QPoint globalPosition() const {
         Q_Q(const QWindow);
index 86d8da2..e756426 100644 (file)
@@ -547,6 +547,9 @@ void QWidgetPrivate::show_sys()
             }
         }
 
+#ifndef QT_NO_CURSOR
+        qt_qpa_set_cursor(q, false); // Needed in case cursor was set before show
+#endif
         invalidateBuffer(q->rect());
         window->setVisible(true);
     }