Widgets: be more careful when setting focus on touch release

An application might choose to change focus when receiving mouse/touch
press/move events. This is in conflict with Qt assigning focus on touch
release (QPlatformIntegration::SetFocusOnTouchRelease), since Qt
might then reassign focus to something else.

An example of this is seen with the "frozencolumn" example. Here, when
the user double clicks on a cell, the application creates an 'edit'
widget inside the cell that gets focus. But at soon as the last release
is sent, Qt will change focus to the focus proxy of QScrollArea instead.

This patch will introduce an exception to setting focus on release, so that
we only set focus if we detect that focus didn't change (by the app)
while processing press/move events.

Task-number: QTBUG-39390
Change-Id: I7b398b59e3175265afd2fcd938e11f88155abc89
Reviewed-by: Laszlo Agocs <laszlo.agocs@theqtcompany.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@theqtcompany.com>
This commit is contained in:
Richard Moe Gustavsen 2014-11-04 11:47:50 +01:00 committed by Shawn Rutledge
parent aca0fb1786
commit 370d421495

View File

@ -4172,11 +4172,13 @@ void QApplicationPrivate::giveFocusAccordingToFocusPolicy(QWidget *widget, QEven
{
const bool setFocusOnRelease = QGuiApplication::styleHints()->setFocusOnTouchRelease();
Qt::FocusPolicy focusPolicy = Qt::ClickFocus;
static QPointer<QWidget> focusedWidgetOnTouchBegin = 0;
switch (event->type()) {
case QEvent::MouseButtonPress:
case QEvent::MouseButtonDblClick:
case QEvent::TouchBegin:
focusedWidgetOnTouchBegin = QApplication::focusWidget();
if (setFocusOnRelease)
return;
break;
@ -4184,6 +4186,11 @@ void QApplicationPrivate::giveFocusAccordingToFocusPolicy(QWidget *widget, QEven
case QEvent::TouchEnd:
if (!setFocusOnRelease)
return;
if (focusedWidgetOnTouchBegin != QApplication::focusWidget()) {
// Focus widget was changed while delivering press/move events.
// To not interfere with application logic, we leave focus as-is
return;
}
break;
case QEvent::Wheel:
focusPolicy = Qt::WheelFocus;