From 68dc5b90c04058ff8d940c093528effa80ac7e4a Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 21 Oct 2011 10:59:27 +0200 Subject: [PATCH] Add virtual QWindow::focusObject() method MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- src/gui/kernel/qwindow.cpp | 9 +++++++++ src/gui/kernel/qwindow.h | 1 + src/widgets/kernel/qwidgetwindow_qpa.cpp | 17 ++++++++++++----- src/widgets/kernel/qwidgetwindow_qpa_p.h | 1 + 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp index 04098b4..80666b8 100644 --- a/src/gui/kernel/qwindow.cpp +++ b/src/gui/kernel/qwindow.cpp @@ -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(this); +} + void QWindow::showMinimized() { diff --git a/src/gui/kernel/qwindow.h b/src/gui/kernel/qwindow.h index 44cbfd0..7d6a391 100644 --- a/src/gui/kernel/qwindow.h +++ b/src/gui/kernel/qwindow.h @@ -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; diff --git a/src/widgets/kernel/qwidgetwindow_qpa.cpp b/src/widgets/kernel/qwidgetwindow_qpa.cpp index 2265fb5..32a446f 100644 --- a/src/widgets/kernel/qwidgetwindow_qpa.cpp +++ b/src/widgets/kernel/qwidgetwindow_qpa.cpp @@ -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() diff --git a/src/widgets/kernel/qwidgetwindow_qpa_p.h b/src/widgets/kernel/qwidgetwindow_qpa_p.h index 9f31d0e..906b96c 100644 --- a/src/widgets/kernel/qwidgetwindow_qpa_p.h +++ b/src/widgets/kernel/qwidgetwindow_qpa_p.h @@ -65,6 +65,7 @@ public: QWidget *widget() const { return m_widget; } QAccessibleInterface *accessibleRoot() const; + QObject *focusObject() const; protected: bool event(QEvent *); -- 2.7.4