[Interest] Quick Item visibility test / hit test... or synthesize press event
Federico Ferri
federico.ferri.it at gmail.com
Wed Jun 22 16:45:31 CEST 2022
Too bad Item.childAt(...) disregards the z value. It would be exactly that
one function I had overlooked and that does the job.
Current behavior of Item.childAt(...) makes no sense, and its documentation
is kind of a lie:
"
Returns the first visible child item found at point (*x*, *y*) within the
coordinate system of this item.
"
It is not visible if it is occluded by an item declared after with a higher
z.
On Wed, Jun 22, 2022 at 4:06 AM EXT Mitch Curtis <mitch.curtis at qt.io> wrote:
> 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-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.
>
> 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/20220622/77cad9f3/attachment.htm>
More information about the Interest
mailing list