<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Courier New";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-AU" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:EN-US">Thanks Andrew, I’ll give that a go.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:EN-US">For anyone who is interested, I’ve created QTBUG-53275 to track this.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Andrew den Exter [mailto:andrew.den.exter@qinetic.com.au]
<br>
<b>Sent:</b> Monday, 9 May 2016 3:18 PM<br>
<b>To:</b> Mitch Curtis <mitch.curtis@qt.io><br>
<b>Cc:</b> development@qt-project.org<br>
<b>Subject:</b> Re: [Development] [Qt Quick] Automatically restoring focus to last focused item<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">I was referring to ApplicationWindow's contentItem which is what you've patched in qtquickcontrols2.  For items within the contentItem to have focus at startup you will also want to give that contentItem focus when it is constructed.  Add
 d->contentItem->setFocus(true) to your patch and pressing alt will work again.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Then to restore focus when the menu is closed I guess in either QQuickPopupPrivate::<span style="color:black">prepareExitTransition() or </span>QQuickPopupPrivate<span style="color:black">::finalizeExitTransition() you want again call setFocus(true)
 on ApplicationWindow's contentItem again.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">This works because making an item a focus scope isolates all of it's children from focus changes around it.  With your example when forceActiveFocus() is called on the menu it removes focus from the contentItem
 scope breaking the active focus chain to focusScope but leaves the focus chain within contentItem intact so focusScope.focus is still true but focusScope.activeFocus is false.  If the menu is closed and setFocus(true) is called on ApplicationWindow.contentItem
 then that restores the missing link in the active focus chain to focusScope and you're back to where you started.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">On Mon, May 9, 2016 at 9:35 PM, Mitch Curtis <<a href="mailto:mitch.curtis@qt.io" target="_blank">mitch.curtis@qt.io</a>> wrote:<o:p></o:p></p>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">Thanks for the reply, Andrew.</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">It looks like the contentItem of QQuickWindow is already a focus scope [1]:</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">    contentItemPrivate->flags |= QQuickItem::ItemIsFocusScope;</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">Shouldn’t that already work, then?</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">If I set QT_LOGGING_RULES=</span>
<span style="font-size:10.0pt;font-family:"Courier New"">qt.quick.focus=true, and use the following example, I get the output below:</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""><a href="http://pastebin.com/X9JMcpGh" target="_blank">http://pastebin.com/X9JMcpGh</a></span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">This is printed when the application has started:</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus: QQuickWindowPrivate::setFocusInScope():</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     scope: QQuickRootItem(0x1e25bec0000)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     scopeSubFocusItem: QObject(0x0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     item: QQuickFocusScope_QML_0(0x1e25bec66c0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     activeFocusItem: QObject(0x0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus: QQuickWindowPrivate::setFocusInScope():</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     scope: QObject(0x0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     item: QQuickRootItem(0x1e25bec0000)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     activeFocusItem: QObject(0x0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qml: activeFocusItem QQuickFocusScope_QML_0(0x1e25bec66c0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">This is printed when I open the menu:</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus: QQuickWindowPrivate::setFocusInScope():</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     scope: QQuickToolBar(0x1e25bec2dc0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     scopeSubFocusItem: QObject(0x0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     item: QQuickToolButton(0x1e25bec2a60)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     activeFocusItem: QQuickFocusScope_QML_0(0x1e25bec66c0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus: QQuickWindowPrivate::setFocusInScope():</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     scope: QQuickRootItem(0x1e25bec0000)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     scopeSubFocusItem: QQuickFocusScope_QML_0(0x1e25bec66c0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     item: QQuickToolBar(0x1e25bec2dc0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     activeFocusItem: QQuickFocusScope_QML_0(0x1e25bec66c0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qml: activeFocusItem QQuickToolButton(0x1e25bec2a60)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qml: @@@@@@@@@ clicked menu toolbutton</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus: QQuickWindowPrivate::setFocusInScope():</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     scope: QQuickRootItem(0x1e25bec0000)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     scopeSubFocusItem: QQuickToolBar(0x1e25bec2dc0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     item: QQuickPopupItem(0x1e25bec29a0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     activeFocusItem: QQuickToolButton(0x1e25bec2a60)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qml: activeFocusItem QQuickPopupItem(0x1e25bec29a0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">And this is printed when I close the menu:</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus: QQuickWindowPrivate::clearFocusInScope():</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     scope: QQuickRootItem(0x1e25bec0000)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     item: QQuickPopupItem(0x1e25bec29a0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qt.quick.focus:     activeFocusItem: QQuickPopupItem(0x1e25bec29a0)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qml: activeFocusItem QQuickRootItem(0x1e25bec0000)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      qml: @@@@@@@@@ closed menu</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">If I apply the following patch to qtquickcontrols2, the root item becomes the active focus item, and so pressing alt does
 nothing:</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      diff --git a/src/quicktemplates2/qquickapplicationwindow.cpp b/src/quicktemplates2/qquickapplicationwindow.cpp</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      index 401313d..cb8a2fa 100644</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      --- a/src/quicktemplates2/qquickapplicationwindow.cpp</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      +++ b/src/quicktemplates2/qquickapplicationwindow.cpp</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      @@ -424,6 +424,7 @@ QQuickItem *QQuickApplicationWindow::contentItem() const</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">            QQuickApplicationWindowPrivate *d = const_cast<QQuickApplicationWindowPrivate *>(d_func());</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">            if (!d->contentItem) {</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">                  d->contentItem = new QQuickItem(QQuickWindow::contentItem());</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">      +            d->contentItem->setFlag(QQuickItem::ItemIsFocusScope);</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">                  d->relayout();</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">            }</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">            return d->contentItem;</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New"">[1]
<a href="http://code.qt.io/cgit/qt/qtdeclarative.git/tree/src/quick/items/qquickwindow.cpp#n483" target="_blank">
http://code.qt.io/cgit/qt/qtdeclarative.git/tree/src/quick/items/qquickwindow.cpp#n483</a></span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:"Courier New""> </span><o:p></o:p></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Andrew
 den Exter [mailto:<a href="mailto:andrew.den.exter@qinetic.com.au" target="_blank">andrew.den.exter@qinetic.com.au</a>]
<br>
<b>Sent:</b> Friday, 6 May 2016 1:38 PM<br>
<b>To:</b> Mitch Curtis <<a href="mailto:mitch.curtis@qt.io" target="_blank">mitch.curtis@qt.io</a>><br>
<b>Cc:</b> <a href="mailto:development@qt-project.org" target="_blank">development@qt-project.org</a><br>
<b>Subject:</b> Re: [Development] [Qt Quick] Automatically restoring focus to last focused item</span><o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">That's a very universal solution to a specific problem and one that's going to have all kinds of unintended consequences. Without knowing why focus was taken from an item and given
 to another and why that other object relinquished focus it's impossible to know whether focus should be restored to that first item.  That memory may have to go back more than one item as well, after all if you open one menu then another then the previous
 focus item is the first window and that's obviously not what you want to give focus to.<o:p></o:p></p>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">What you want to do instead is introduce a FocusScope to ApplicationWindow. If the window's content item is a focus scope then giving focus to a menu which is outside of this scope
 will not affect focus within the scope, then whatever logic closes the menu can give focus back to the content item and that will implicitly return active focus to your item in the window body.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Andrew<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">On Fri, May 6, 2016 at 12:08 AM, Mitch Curtis <<a href="mailto:mitch.curtis@qt.io" target="_blank">mitch.curtis@qt.io</a>> wrote:<o:p></o:p></p>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0cm;margin-bottom:5.0pt">
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Consider the example below (requires Qt 5.7):<br>
<br>
    import QtQuick 2.6<br>
    import QtQuick.Layouts 1.1<br>
    import QtQuick.Window 2.2<br>
    import QtQuick.Controls 2.0<br>
<br>
    ApplicationWindow {<br>
        width: 400<br>
        height: 200<br>
        visible: true<br>
<br>
        onActiveFocusItemChanged: print("activeFocusItem", activeFocusItem)<br>
<br>
        header: ToolBar {<br>
            RowLayout {<br>
                focus: false<br>
                implicitWidth: children[0].implicitWidth<br>
                implicitHeight: children[0].implicitHeight<br>
<br>
                ToolButton {<br>
                    text: qsTr("File")<br>
                    onClicked: fileMenu.open()<br>
<br>
                    Menu {<br>
                        id: fileMenu<br>
                        y: parent.height<br>
<br>
                        MenuItem {<br>
                            text: qsTr("New")<br>
                        }<br>
                        MenuItem {<br>
                            text: qsTr("open")<br>
                        }<br>
                        MenuItem {<br>
                            text: qsTr("Close")<br>
                        }<br>
                    }<br>
                }<br>
            }<br>
        }<br>
<br>
        FocusScope {<br>
            id: focusScope<br>
            focus: true<br>
            anchors.fill: parent<br>
<br>
            property bool toggled: false<br>
            onToggledChanged: print("toggled", toggled)<br>
<br>
            Keys.onPressed: {<br>
                if (event.modifiers === Qt.AltModifier) {<br>
                    focusScope.toggled = true;<br>
                }<br>
            }<br>
            Keys.onReleased: {<br>
                if ((event.modifiers === Qt.AltModifier || event.key === Qt.Key_Alt) && !event.isAutoRepeat) {<br>
                    focusScope.toggled = false;<br>
                }<br>
            }<br>
<br>
            RowLayout {<br>
                anchors.centerIn: parent<br>
<br>
                Button {<br>
                    id: penButton<br>
                    text: qsTr("Pen")<br>
                    highlighted: !focusScope.toggled<br>
                }<br>
                Button {<br>
                    id: eyedropperButton<br>
                    text: qsTr("Eyedropper")<br>
                    highlighted: focusScope.toggled<br>
                }<br>
            }<br>
        }<br>
    }<br>
<br>
The idea is that holding the Alt key down will toggle between two "modes". The mode is indicated by a highlighted button.<br>
<br>
Try switching between the buttons. Now open the menu by clicking the "File" button. The menu will receive focus and the focus scope will lose it. If you then close the menu and try to switch between the buttons again, it won't work because the focus scope never
 regained focus (the root item now has it). This is explained here [1]:<br>
<br>
"When a QML Item explicitly relinquishes focus (by setting its focus property to false while it has active focus), the system does not automatically select another type to receive focus. That is, it is possible for there to be no currently active focus."<br>
<br>
I'm not sure if "explicitly relinquishes focus" is exactly what's happening here, though. The FocusScope loses focus because something else was open temporarily. So now the developer has no other option than to listen to e.g. the menu visibility changes and
 explicitly set focus on the FocusScope accordingly. That's pretty gross if you ask me. I think we can do better. Specifically, I think that we should remember the last focused item, and give that focus, rather than give it to the root item. The question is,
 would it be an acceptable change for Qt 5? I'd assume that users are already working around this anyway, and so the fix wouldn't hurt.<br>
<br>
[1] <a href="http://doc.qt.io/qt-5/qtquick-input-focus.html" target="_blank">http://doc.qt.io/qt-5/qtquick-input-focus.html</a><br>
_______________________________________________<br>
Development mailing list<br>
<a href="mailto:Development@qt-project.org" target="_blank">Development@qt-project.org</a><br>
<a href="http://lists.qt-project.org/mailman/listinfo/development" target="_blank">http://lists.qt-project.org/mailman/listinfo/development</a><o:p></o:p></p>
</blockquote>
</div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>
</body>
</html>