From 18f9eb797bffe8626f1edeca3c88f80dae0da8d7 Mon Sep 17 00:00:00 2001 From: Jan Arve Saether Date: Tue, 19 Feb 2013 15:02:24 +0100 Subject: QStackedLayout: Fix crash when focus widget is destroyed in hide() We also have to make sure that when moving back to a page that has a focusWidget(), the focus should go to the focusWidget() Task-number: QTBUG-18242 Change-Id: Ibfa7d6361c1a456480b2f1584a88ef4c4f405709 Reviewed-by: Friedemann Kleint --- src/widgets/kernel/qstackedlayout.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'src/widgets/kernel/qstackedlayout.cpp') diff --git a/src/widgets/kernel/qstackedlayout.cpp b/src/widgets/kernel/qstackedlayout.cpp index cb2711c636..f38f0a6f08 100644 --- a/src/widgets/kernel/qstackedlayout.cpp +++ b/src/widgets/kernel/qstackedlayout.cpp @@ -300,7 +300,9 @@ void QStackedLayout::setCurrentIndex(int index) parent->setUpdatesEnabled(false); } - QWidget *fw = parent ? parent->window()->focusWidget() : 0; + QPointer fw = parent ? parent->window()->focusWidget() : 0; + const bool focusWasOnOldPage = fw && (prev && prev->isAncestorOf(fw)); + if (prev) { prev->clearFocus(); if (d->stackingMode == StackOne) @@ -315,24 +317,25 @@ void QStackedLayout::setCurrentIndex(int index) // was somewhere on the outgoing widget. if (parent) { - if (fw && (prev && prev->isAncestorOf(fw))) { // focus was on old page + if (focusWasOnOldPage) { // look for the best focus widget we can find if (QWidget *nfw = next->focusWidget()) nfw->setFocus(); else { // second best: first child widget in the focus chain - QWidget *i = fw; - while ((i = i->nextInFocusChain()) != fw) { - if (((i->focusPolicy() & Qt::TabFocus) == Qt::TabFocus) - && !i->focusProxy() && i->isVisibleTo(next) && i->isEnabled() - && next->isAncestorOf(i)) { - i->setFocus(); - break; + if (QWidget *i = fw) { + while ((i = i->nextInFocusChain()) != fw) { + if (((i->focusPolicy() & Qt::TabFocus) == Qt::TabFocus) + && !i->focusProxy() && i->isVisibleTo(next) && i->isEnabled() + && next->isAncestorOf(i)) { + i->setFocus(); + break; + } } + // third best: incoming widget + if (i == fw ) + next->setFocus(); } - // third best: incoming widget - if (i == fw ) - next->setFocus(); } } } -- cgit v1.2.3