qt - QML - How to know if a child has keyboard focus -
i think know how use focusscopes , how handle keyboard focus.
but can't find clever way figure out if 1 of child items or theirs or below me has keyboard focus.
the documentation focusscope says:
when focus scope receives active focus, contained element focus set (if any) gets active focus. if element focusscope, proxying behavior continues. both focus scope , sub-focused item have activefocus property set.
a focusscope therefore will have activefocus set false when focus given contained focusscope. there way figure out if case? how can know if @ least contained focusscope received focus?
focus chain in qtquick. means ancestor focusscope down current active child active focus.
focusscope used make more simple focus abstraction : tell custom component when root object gets active focus, has forward given child.
in following example :
import qtquick 2.0; rectangle { width: 400; height: 200; focus: true; focusscope { id: scope1; anchors { top: parent.top; left: parent.left; right: parent.right; bottom: parent.verticalcenter; } rectangle { id: rect1; color: (scope1.activefocus ? "yellow" : "gray"); border.width: 1; anchors.fill: parent; mousearea { anchors.fill: parent; onclicked: { scope1.forceactivefocus (); } } textinput { id: input1; focus: true; anchors.centerin: parent; } } } focusscope { id: scope2; anchors { top: parent.verticalcenter; left: parent.left; right: parent.right; bottom: parent.bottom; } rectangle { id: rect2; color: (scope2.activefocus ? "yellow" : "gray"); border.width: 1; anchors.fill: parent; mousearea { anchors.fill: parent; onclicked: { scope2.forceactivefocus (); } } textinput { id: input2; focus: true; anchors.centerin: parent; } } } }
... want 2 big areas can have focus , don't necessarly want focus explicitely inner textinput (cause ideally inside custom component, not accessible outside).
so when area gets clicked, give active focus parent scope, , scope automatically proxies child has focus:true flag (means wants focus, not has it, that's why have 1 flag in each textinput).
the items need know if inner input has active focus instead request if scope has it. don't have care inside.
if scope contains scope focus:true, focus forwarded again until reaches latest item wants focus.
Comments
Post a Comment