<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Using PinchArea and MouseArea on the Canvas element]]></title><description><![CDATA[<p dir="auto">Synfony about 4 hours ago</p>
<p dir="auto">Hi,<br />
I building a QtQuick app for the (Windows 10) Surface tablet.<br />
It's a sketching application where we are using mouse/Pen along with Touch gestures.<br />
So, I have placed the Canvas element with PinchArea and inside the PinchArea there is the MouseArea.<br />
Both Pinching and Mouse/Pen events are working except for the following glitch:<br />
I see that when I try to Pinch with fingers the first event that gets fired is the Mouse's onPressed event and then the pinch zoom happens.<br />
How do I stop the mouse onPressed getting fired in this case?<br />
I need to disable mouse event entirely when I am using my fingers to zoom.<br />
Here is the code snippet I am using:</p>
<pre><code>import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Canvas {
        id: myCanvas
        anchors.fill: parent

        property int pressedValue: 0
        property int pinchFlag: 0

        onPaint: {
            var ctx = getContext("2d");
            ctx.fillStyle = Qt.rgba(1, 0, 0, 1);

            ctx.moveTo(75, 50);
            ctx.lineTo(100, 75);
            ctx.lineTo(120, 50);
            ctx.lineTo(150, 50);
            ctx.lineTo(150, 25);
            ctx.lineTo(5, 15);
            ctx.fill();
        }
        PinchArea{
            anchors.fill: parent
            pinch.target: myCanvas
            pinch.maximumScale: 10.0
            pinch.minimumScale: 0.1
            pinch.maximumRotation: 0 //360
            pinch.minimumRotation: 0 //-360
            pinch.dragAxis: Pinch.XAndYAxis

            onPinchStarted: {
                if (pinch.active)
                    myCanvas.pinchFlag = 1
            }
            onPinchFinished: {
                myCanvas.pinchFlag = 0
            }

            MouseArea {
                anchors.fill: parent
                acceptedButtons: Qt.AllButtons

                onPressed: {
                    if(myCanvas.pinchFlag ===0){
                        myCanvas.pressedValue = myCanvas.pressedValue +1
                        console.log("Pressed: " + myCanvas.pressedValue)
                    }
                }
            }//end:MouseArea
        }//end: PinchArea
    }//end:Canvas
}

</code></pre>
<p dir="auto">I am really struggling with this. Please provide your help.<br />
Thank you.</p>
]]></description><link>https://forum.qt.io/topic/114739/using-pincharea-and-mousearea-on-the-canvas-element</link><generator>RSS for Node</generator><lastBuildDate>Mon, 16 Nov 2026 07:18:02 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/114739.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 12 May 2020 20:31:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Using PinchArea and MouseArea on the Canvas element on Tue, 14 Feb 2023 10:46:33 GMT]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">I've had a similar problem and solved it by storing the mouse position in the <code>onPressed</code> event, but using it only when <code>onPressAndHold</code> and <code>onReleased</code> is called as in the following code example:</p>
<pre><code>import QtQuick 2.11

Item {
    id: root

    Rectangle {
        id: drawingArea

        anchors.fill: parent

        PinchArea {
            id: pinchArea
            anchors.fill: parent

            property bool isDragging: false

            pinch {
                target: pattern
                dragAxis: Pinch.XAndYAxis
            }

            onPinchStarted: pinchArea.isDragging = true
            onPinchFinished: pinchArea.isDragging = false
        } // end:PinchArea

        Rectangle {
            id: pattern
            color: 'blue'
            width: 20 
            height: 20
            x: 10
            y: 10
        }

        MouseArea {
            id: mouseArea
            property var _initialEvent: undefined
            enabled: ! pinchArea.isDragging
            anchors.fill: parent

            onPressed: _initialEvent = Qt.point(mouse.x, mouse.y)
            onPressAndHold: doStuff(Qt.point(mouse.x, mouse.y))
            onPositionChanged: doStuff(Qt.point(mouse.x, mouse.y))
            onReleased: doStuff(Qt.point(mouse.x, mouse.y))

            // nothing to do, took over by pinchArea
            onCanceled: _initialEvent = undefined

            function doStuff(position) {
                if (mouseArea._initialEvent !== undefined &amp;&amp; mouseArea.enabled) {
                    console.log('Doing stuff with ', mouseArea._initialEvent.x, ':', mouseArea._initialEvent.y);
                }
                mouseArea._initialEvent = undefined;
                console.log('Doing stuff with ', position.x, ':', position.y);
            }
        }// end: MouseArea
    }
}
</code></pre>
<p dir="auto">Simon</p>
]]></description><link>https://forum.qt.io/post/747571</link><guid isPermaLink="true">https://forum.qt.io/post/747571</guid><dc:creator><![CDATA[Simon Fojtu]]></dc:creator><pubDate>Tue, 14 Feb 2023 10:46:33 GMT</pubDate></item><item><title><![CDATA[Reply to Using PinchArea and MouseArea on the Canvas element on Wed, 13 May 2020 12:07:17 GMT]]></title><description><![CDATA[<p dir="auto">Hi,<br />
I asked the same thing a month ago: <a href="https://forum.qt.io/topic/113763/pincharea-and-mousearea">https://forum.qt.io/topic/113763/pincharea-and-mousearea</a><br />
No answer yet...</p>
]]></description><link>https://forum.qt.io/post/594448</link><guid isPermaLink="true">https://forum.qt.io/post/594448</guid><dc:creator><![CDATA[DeltaSim]]></dc:creator><pubDate>Wed, 13 May 2020 12:07:17 GMT</pubDate></item></channel></rss>