Focus event fix for QQuickWidget
authorPaul Olav Tvete <paul.tvete@digia.com>
Wed, 21 May 2014 08:49:07 +0000 (10:49 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Wed, 21 May 2014 10:35:21 +0000 (12:35 +0200)
Relay focus in/out events to the offscreen QQuickWindow, and also
make the offscreen window believe it has the focus when the
render window has it.

Task-number: QTBUG-39033
Change-Id: Ib50b134e635833ad3813693ca272f04607c525b8
Reviewed-by: Laszlo Agocs <laszlo.agocs@digia.com>
src/quick/items/qquickwindow.cpp
src/quickwidgets/qquickwidget.cpp
src/quickwidgets/qquickwidget.h

index 6220ea2..e78f914 100644 (file)
@@ -687,6 +687,13 @@ void QQuickWindowPrivate::translateTouchEvent(QTouchEvent *touchEvent)
     touchEvent->setTouchPoints(touchPoints);
 }
 
+
+static inline bool windowHasFocus(QQuickWindow *win)
+{
+    const QWindow *focusWindow = QGuiApplication::focusWindow();
+    return win == focusWindow || QQuickRenderControl::renderWindowFor(win) == focusWindow;
+}
+
 /*!
 Set the focus inside \a scope to be \a item.
 If the scope contains the active focus item, it will be changed to \a item.
@@ -762,7 +769,7 @@ void QQuickWindowPrivate::setFocusInScope(QQuickItem *scope, QQuickItem *item, Q
     }
 
     if (!(options & DontChangeFocusProperty)) {
-        if (item != contentItem || QGuiApplication::focusWindow() == q) {
+        if (item != contentItem || windowHasFocus(q)) {
             itemPrivate->focus = true;
             changed << item;
         }
index e20e5f2..7e31c27 100644 (file)
@@ -941,6 +941,20 @@ void QQuickWidget::wheelEvent(QWheelEvent *e)
 }
 #endif
 
+
+void QQuickWidget::focusInEvent(QFocusEvent * event)
+{
+    Q_D(QQuickWidget);
+    d->offscreenWindow->focusInEvent(event);
+}
+
+void QQuickWidget::focusOutEvent(QFocusEvent * event)
+{
+    Q_D(QQuickWidget);
+    d->offscreenWindow->focusOutEvent(event);
+}
+
+
 /*! \reimp */
 bool QQuickWidget::event(QEvent *e)
 {
index 81e9af0..4287933 100644 (file)
@@ -120,6 +120,9 @@ protected:
     virtual void showEvent(QShowEvent *);
     virtual void hideEvent(QHideEvent *);
 
+    virtual void focusInEvent(QFocusEvent * event);
+    virtual void focusOutEvent(QFocusEvent * event);
+
 #ifndef QT_NO_WHEELEVENT
     virtual void wheelEvent(QWheelEvent *);
 #endif