Qt Quick QML MouseArea 按下时自动重复

Qt Quick QML MouseArea autorepeat on press

希望我没有遗漏任何明显的东西。

我正在编写一个应用程序,并制作了一个带有 ImageMouseArea 的放大按钮。我需要按钮在按住鼠标按钮的同时每秒重复一次方法调用以放大。如何重复这种情况并不十分明显。现在我有:

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); }
    }

我也试过

onPressAndHold: { cameraController.zoomIn(0.5); }

效果基本相同,虽然有预期的小延迟,但我需要在按住鼠标按钮时每秒重复一次此操作。

要执行您需要的任务,如果计时器已激活,您必须立即使用 Timer. the timer must remain active while the containsMouse is active. you must also enable triggeredOnStart 到 运行。

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
        }
    }
}