[Interest] Quick Item visibility test / hit test... or synthesize press event

Federico Ferri federico.ferri.it at gmail.com
Tue Jun 21 23:23:18 CEST 2022


I have solved it more or less with this:

    function traverseItemTreeWithClipping(item, pt, f, rootItem) {

        rootItem = rootItem || item

        var p = item.mapFromItem(rootItem, pt)

        var inItemRect = p.x >= 0 && p.y >= 0 && p.x < item.width &&
p.y < item.height

        if(item.clip && !inItemRect) return

        f(item, p, inItemRect)

        for(var i = 0; i < item.children.length; i++)

            traverseItemTreeWithClipping(item.children[i], pt, f, rootItem)

    }


    function hitTest(rootItem, pt) {

        var result = undefined

        var z = -Infinity

        traverseItemTreeWithClipping(rootItem, pt, (item, p, inItemRect) => {

            if(item.visible && item.z >= z && inItemRect) {

                result = item

                z = item.z

            }

        })

        return result

    }


It only handles visibility and clipping. If there is a better way or I
overlooked some QtQuick aspect, please let me know.

Cheers,

Federico Ferri



On Tue, Jun 21, 2022 at 8:54 PM Federico Ferri <federico.ferri.it at gmail.com>
wrote:

> Is it possible to find what is the topmost visible item given a pixel
> coordinate?
>
> e.g. in this simplified example:
>
> Rectangle {
>     x: 20; y: 20; width: 20; height: 20; color: 'red';
>     Rectangle {
>         x: 5; y: 10; width: 5; height: 5; color: 'green';
>     }
> }
>
> it would be the red Rectangle for coordinate (21,21), the green Rectangle
> for coordinate (26,32), and none for e.g. (15,15).
>
> Alternatively, I'm happy to equip my Rectangles with a MouseArea
> {anchors.fill:parent} each. Is it possible then to synthesize mouse press
> events so to exploit Qt Quick's scenegraph/event processing magic?
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20220621/7226f251/attachment.htm>


More information about the Interest mailing list