Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

forceActiveFocus() vs focus = true in QML

Tags:

focus

qt

qml

I read the documentation about:

  • focus property
  • activeFocus property
  • forceActiveFocus() method
  • FocusScope object
  • and keyboard focus in QtQuick

but it is still not clear when should someone use forceActiveFocus() method over setting the focus property to true or vice versa.

like image 453
Silex Avatar asked Apr 19 '17 06:04

Silex


People also ask

What is focus in QML?

An Item requests focus by setting the focus property to true . For very simple cases simply setting the focus property is sometimes sufficient. If we run the following example with the qml tool, we see that the keyHandler type has active focus and pressing the A , B , or C keys modifies the text appropriately.

What is Z in QML?

Now QML gives you chance to change the stacking order through z property of the item. in the above example if I assign z property of the red rectangle to have a value of anything above 0, I would see it on top of blue rectangle. So z property has changed the stacking order for the sibling item.

What is loader in QML?

Loader is used to dynamically load QML components. Loader can load a QML file (using the source property) or a Component object (using the sourceComponent property).

What is parent in QML?

All QML objects have an object parent, which is determined by the object hierarchy in which the object is declared.


1 Answers

As the documentation states:

For very simple cases simply setting the focus property is sometimes sufficient.

Such a simple case would be, if the Item which gets focus: true is not enclosed by a FocusScope that might have not the focus.

Then it continues with:

> Within each focus scope one object may have Item::focus set to true. If more than one Item has the focus property set, the last type to set the focus will have the focus and the others are unset, similar to when there are no focus scopes.

> When a focus scope receives active focus, the contained type with focus set (if any) also gets the active focus. If this type is also a FocusScope, the proxying behavior continues. Both the focus scope and the sub-focused item will have the activeFocus property set.

From where we learn, about the fact that setting focus: true is not sufficient, if it is for an Item that is a successor to a FocusScope as this FocusScope would need to have activeFocus su that the successor Item will receive activeFocus. This is recursive, and means, that the FocusScope will need to have focus: true and a possible predecessor FocusScope needs the activeFocus and so on. Which results in some kind of focus tree

This focus tree consists out of inner nodes that are the FocusScopes and leafs that are Items. A FocusScope might be a leaf as well, but I don't know why it should.

In this tree, each FocusScope may have up to one child node (either Item (leaf) or FocusScope (inner node) that has focus === true. Traversing this tree, following the path where all traversed nodes have focus === true the traversed nodes have also activeFocus === true. As each FocusScope may have only up to one child node with focus === true there is only one such path.

Column {
    FocusScope {
        focus: false
        width: 100
        height: 100
        Text {
            focus: true
            text: 'has focus ' + focus + '\nhas activeFocus ' + activeFocus
        }
    }
    FocusScope {
        focus: true
        width: 100
        height: 100
        Text {
            focus: true
            text: 'has focus ' + focus + '\nhas activeFocus ' + activeFocus
        }
    }
}

Here we have two FocusScopes. Both have a child that has focus, but as only the second FocusScope has the focus itself, it's child has activeFocus.

The use of forceActiveFocus() traverses the focus tree, and sets focus to true for each node on the way, so the Item has activeFocus at the end.

like image 185
derM Avatar answered Oct 20 '22 11:10

derM