setting the initial focus only on Qt::PopupFocusReason

This commit is contained in:
Uwe Rathmann 2018-02-04 12:28:26 +01:00
parent d98317942e
commit 84d8c87309
2 changed files with 39 additions and 0 deletions

View File

@ -322,6 +322,42 @@ bool QskPopup::event( QEvent* event )
return ok; 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 QQuickItem* QskPopup::focusSuccessor() const
{ {
if ( const auto scope = qskNearestFocusScope( this ) ) if ( const auto scope = qskNearestFocusScope( this ) )

View File

@ -38,7 +38,10 @@ Q_SIGNALS:
protected: protected:
virtual void updateLayout() override; virtual void updateLayout() override;
virtual bool event( QEvent* ) override; virtual bool event( QEvent* ) override;
virtual void focusInEvent( QFocusEvent * ) override;
virtual void focusOutEvent( QFocusEvent * ) override;
virtual void itemChange( QQuickItem::ItemChange, virtual void itemChange( QQuickItem::ItemChange,
const QQuickItem::ItemChangeData& ) override; const QQuickItem::ItemChangeData& ) override;