Add virtual QWindow::focusObject() method
authorLars Knoll <lars.knoll@nokia.com>
Fri, 21 Oct 2011 08:59:27 +0000 (10:59 +0200)
committerQt by Nokia <qt-info@nokia.com>
Sat, 22 Oct 2011 17:18:20 +0000 (19:18 +0200)
The method allows to retrieve the object that currently
has the input focus inside the Window. This is e.g.
required to correctly determine the context for keyboard
shortcuts.

Change-Id: I9e05ef62717973bac275ce34cc70fb86aa2d1e5b
Reviewed-by: Samuel Rødal <samuel.rodal@nokia.com>
src/gui/kernel/qwindow.cpp
src/gui/kernel/qwindow.h
src/widgets/kernel/qwidgetwindow_qpa.cpp
src/widgets/kernel/qwidgetwindow_qpa_p.h

index 04098b4..80666b8 100644 (file)
@@ -663,6 +663,15 @@ QAccessibleInterface *QWindow::accessibleRoot() const
     return 0;
 }
 
+/*!
+  Returns the QObject that will be the final receiver of events tied focus, such
+  as key events.
+*/
+QObject *QWindow::focusObject() const
+{
+    return const_cast<QWindow *>(this);
+}
+
 
 void QWindow::showMinimized()
 {
index 44cbfd0..7d6a391 100644 (file)
@@ -196,6 +196,7 @@ public:
     void setScreen(QScreen *screen);
 
     virtual QAccessibleInterface *accessibleRoot() const;
+    virtual QObject *focusObject() const;
 
     QPoint mapToGlobal(const QPoint &pos) const;
     QPoint mapFromGlobal(const QPoint &pos) const;
index 2265fb5..32a446f 100644 (file)
@@ -67,6 +67,16 @@ QAccessibleInterface *QWidgetWindow::accessibleRoot() const
     return 0;
 }
 
+QObject *QWidgetWindow::focusObject() const
+{
+    QWidget *widget = m_widget->focusWidget();
+
+    if (!widget)
+        widget = m_widget;
+
+    return widget;
+}
+
 bool QWidgetWindow::event(QEvent *event)
 {
     switch (event->type()) {
@@ -287,12 +297,9 @@ void QWidgetWindow::handleKeyEvent(QKeyEvent *event)
     if (QApplicationPrivate::instance()->modalState() && !qt_try_modal(m_widget, event->type()))
         return;
 
-    QWidget *widget = m_widget->focusWidget();
-
-    if (!widget)
-        widget = m_widget;
+    QObject *receiver = focusObject();
 
-    QGuiApplication::sendSpontaneousEvent(widget, event);
+    QGuiApplication::sendSpontaneousEvent(receiver, event);
 }
 
 void QWidgetWindow::updateGeometry()
index 9f31d0e..906b96c 100644 (file)
@@ -65,6 +65,7 @@ public:
     QWidget *widget() const { return m_widget; }
     QAccessibleInterface *accessibleRoot() const;
 
+    QObject *focusObject() const;
 protected:
     bool event(QEvent *);