From 84d8c87309ab3024e020fe69473a77c27dc4de64 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Sun, 4 Feb 2018 12:28:26 +0100 Subject: [PATCH] setting the initial focus only on Qt::PopupFocusReason --- src/controls/QskPopup.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/controls/QskPopup.h | 3 +++ 2 files changed, 39 insertions(+) diff --git a/src/controls/QskPopup.cpp b/src/controls/QskPopup.cpp index 2508b5f9..db966134 100644 --- a/src/controls/QskPopup.cpp +++ b/src/controls/QskPopup.cpp @@ -322,6 +322,42 @@ bool QskPopup::event( QEvent* event ) return ok; } +void QskPopup::focusInEvent( QFocusEvent* event ) +{ + Inherited::focusInEvent( event ); + + if ( isFocusScope() && isTabFence() && ( scopedFocusItem() == nullptr ) ) + { + if ( event->reason() == Qt::PopupFocusReason ) + { + /* + When receiving the focus we need to have a focused + item, so that the tab focus chain has a starting point. + + But we only do it when the reason is Qt::PopupFocusReason + as we also receive focus events during the process of reparenting + children and setting the focus there can leave the item tree + in an invalid state. + */ + + if ( auto focusItem = nextItemInFocusChain( true ) ) + { + if ( qskIsItemComplete( focusItem ) + && qskIsAncestorOf( this, focusItem ) ) + { + focusItem->setFocus( true ); + } + } + } + } + +} + +void QskPopup::focusOutEvent( QFocusEvent* event ) +{ + Inherited::focusOutEvent( event ); +} + QQuickItem* QskPopup::focusSuccessor() const { if ( const auto scope = qskNearestFocusScope( this ) ) diff --git a/src/controls/QskPopup.h b/src/controls/QskPopup.h index 41f98fb3..63bd46ca 100644 --- a/src/controls/QskPopup.h +++ b/src/controls/QskPopup.h @@ -38,7 +38,10 @@ Q_SIGNALS: protected: virtual void updateLayout() override; + virtual bool event( QEvent* ) override; + virtual void focusInEvent( QFocusEvent * ) override; + virtual void focusOutEvent( QFocusEvent * ) override; virtual void itemChange( QQuickItem::ItemChange, const QQuickItem::ItemChangeData& ) override;