There’s no public API for this specific task, but there have been suggestions 
for it:

https://bugreports.qt.io/browse/QTBUG-23745
https://bugreports.qt.io/browse/QTBUG-83647


From: Interest <interest-boun...@qt-project.org> on behalf of Federico Ferri 
<federico.ferri...@gmail.com>
Date: Wednesday, 22 June 2022 at 05:26
To: Qt Project MailingList <interest@qt-project.org>
Subject: Re: [Interest] Quick Item visibility test / hit test... or synthesize 
press event
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...@gmail.com<mailto:federico.ferri...@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?


_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to