Cinder Biscuits
Cinder Biscuits

Reputation: 5259

Qt Quick QML MouseArea autorepeat on press

Hopefully I'm not missing something obvious here.

I am writing an app and have made a zoom in button with an Image and a MouseArea. I need the button to repeat a method call after, say, every second to zoom in while holding the mouse button down. It isn't entirely obvious how to make this repeat. Right now I have:

Rectangle {
    id:zoomInBtn
    Image {
        id: zoomInImg
        anchors.centerIn: parent
        fillMode: Image.PreserveAspectFit
        source: zoomIn.pressed ? ":/img/zoom_in_sel" : ":/img/zoom_in_unsel"
    }
    MouseArea {
        id: zoomIn
        anchors.fill: parent
        onPressed: { cameraController.zoomIn(0.5); }
    }

I have also tried with

onPressAndHold: { cameraController.zoomIn(0.5); }

which does basically the same, although with a small delay as expected, but I need to repeat this action every second while the mouse button is held.

Upvotes: 2

Views: 1835

Answers (1)

eyllanesc
eyllanesc

Reputation: 244301

To perform the task you need you must use a Timer. the timer must remain active while the containsMouse is active. you must also enable triggeredOnStart to run immediately if the timer is activated.

Rectangle {
    id:zoomInBtn
    Image {
        id: zoomInImg
        anchors.centerIn: parent
        fillMode: Image.PreserveAspectFit
        source: zoomIn.pressed ? ":/img/zoom_in_sel" : ":/img/zoom_in_unsel"
    }
    MouseArea {
        id: zoomIn
        anchors.fill: parent
    }

    Timer {
        id: timer
        interval: 1000
        repeat: true
        triggeredOnStart: true
        running: zoomIn.containsMouse
        onTriggered: cameraController.zoomIn(0.5) //task
        }
    }
}

Upvotes: 3

Related Questions