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:

    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.