Setting focus. Differences between focus and activeFocus properties. Help for the pratcial example
-
Hello gentlemen,
Could someone please explain me the difference between Item.focus and Item.activeFocus properties?
I noticed that focus is writeable while activeFocus is read-only.
Still why isn’t it enough to read focus property to learn the focus status?I would like also to learn if setting Item.focus = true is identical to Item.forceActiveFocus().
I took a closer look at those properties trying to solve the following practical problem.
I want to implement popup which … pops up when I long-press an item in the ListView.
While scrolling between items it is desirable to have popup update with some item related info and appear at the position of the currently pressed item while have the previous copy disappear.
This popup overlaps corresponding item (!)This is my snippet in brief:
@
Component {
id: friendsDelegate
Item {
id: wrapper
…
}
}MouseArea {
Id:mouseArea
anchors.fill: wrapper
hoverEnabled: true
onEntered: {
friends.currentIndex = index
}
onExited: {
popup.state = "invisible"
}onPressAndHold: { popup.state = "visible" } } ListView {
Id: friends
delegate: friendsDelegate
…
}
PopUp {
id: popup
state: "invisible"
}@When we move between items popup disappear and then reappear in a new position which is fine. The problem shows when I try to press on popup which overlaps the item - at this moment popup disappears.
This behavior seems strange as MouseArea.anchors.fill = wrapper, and not just the its foremost part. So it seems that we are not exiting the component.
Why would the popup disappear?I have tried several approaches to overcome this behavior, namely:
A. In Component:
@MouseArea {
Id:mouseArea
onExited: {
if (popup.activeFocus == true)
return;
popup.state = ""
} @B. In Popup:
@MouseArea { anchors.fill: parent onEntered: { popup.state = "visible" }
}@
But they do not bring any luck.
I would highly appreciate any minor advice as my primary goal is to learn and understand.
Thank you guys.
-
Hi,
If you haven't seen them already, here are some of the relevant documentation links:
- http://doc.qt.nokia.com/4.7-snapshot/qdeclarativefocus.html (though note the discussion here: http://lists.qt.nokia.com/pipermail/qt-qml/2010-December/001961.html)
- http://doc.qt.nokia.com/4.7-snapshot/qml-item.html#focus-prop
- http://doc.qt.nokia.com/4.7-snapshot/qml-item.html#activeFocus-prop
The important thing to understand is how focus scopes work. If you still have additional questions about these properties please let me know (hopefully it will give us some ideas of how we could improve the docs).
In the Qt source tree, examples/declarative/keyinteraction/focus gives some examples of working with focus in QML. For your particular example, it might also be helpful to note that ListView behaves as a focus scope.
Regards,
Michael