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

EXT Mitch Curtis mitch.curtis at qt.io
Wed Jun 22 04:06:55 CEST 2022

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


From: Interest <interest-bounces at qt-project.org> on behalf of Federico Ferri <federico.ferri.it at gmail.com>
Date: Wednesday, 22 June 2022 at 05:26
To: Qt Project MailingList <interest at 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.


Federico Ferri

On Tue, Jun 21, 2022 at 8:54 PM Federico Ferri <federico.ferri.it at gmail.com<mailto: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/20220622/a740268c/attachment.htm>

More information about the Interest mailing list