Guest User

Untitled

a guest
Feb 13th, 2026
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 1.72 KB | None | 0 0
  1. import QtQuick
  2.  
  3. MouseArea {
  4.     id: root
  5.  
  6.     // signal drag(real dx, real dy)
  7.     signal anchorDrag(real dx, real dy)
  8.  
  9.     QtObject {
  10.         id: priv
  11.         property bool firstEvent: false
  12.         property real firstx: 0
  13.         property real firsty: 0
  14.     }
  15.  
  16.     Rectangle {
  17.         anchors.fill: parent
  18.         color: "#20000000"
  19.     }
  20.  
  21.     function fpos(p) {
  22.         return `${p.x.toFixed(2)},${p.y.toFixed(2)}`
  23.     }
  24.  
  25.     onPressed: (mouse) => {
  26.                    priv.firstEvent = true
  27.                    console.log(`${fpos(mouse)} press`)
  28.                }
  29.     onReleased: (mouse) => {
  30.                     priv.firstEvent = false
  31.                     console.log(`${fpos(mouse)} release`)
  32.                 }
  33.  
  34.     onPositionChanged: (mouse) => {
  35.                            if (priv.firstEvent) {
  36.                                priv.firstEvent = false
  37.                                priv.firstx = mouse.x
  38.                                priv.firsty = mouse.y
  39.                                let p = root.mapToGlobal(mouse.x, mouse.y)
  40.                                console.log(`${fpos(mouse)} pos change, first event, item ${fpos(mouse)}, global ${fpos(p)}`)
  41.                                return
  42.                            }
  43.                            let dx = mouse.x - priv.firstx
  44.                            let dy = mouse.y - priv.firsty
  45.                            root.anchorDrag(dx, dy)
  46.                            let d = { x: dx, y: dy }
  47.  
  48.                            let p = root.mapToGlobal(priv.firstx, priv.firsty)
  49.                            mousePos.set(p.x, p.y)
  50.                            console.log(`${fpos(mouse)} pos change, d ${fpos(d)}, set to global ${fpos(p)}`)
  51.                        }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment