如何在Qml中做一个动作,每次用户触摸屏幕

How to do an action in Qml every time a user touches the screen?

本文关键字:用户 屏幕 触摸屏 触摸 一个 Qml      更新时间:2023-10-16

我正在用Qt和Qml编写一个嵌入式linux触摸屏设备的应用程序。
我需要实现一个锁定屏幕,出现在30秒的不活动。
为此,我在c++中实现了一个计时器,它在超时后改变程序的状态。

是否有可能刷新这个定时器在全局范围内每次用户触摸屏幕,这样我就不必调用定时器的start()插槽在我的程序中的每个可触摸的元素?

我还想刷新这个计时器,即使当用户触摸屏幕的一部分,那里没有按钮/交互元素。

像这样main.qml:

Rectangle {
    id: mainRect
    width: 800
    height: 480
//something like this (oversimplified) pseudo code:
    onGlobalUserTouch {
        timers.startLockTimer()
    }
//end pseudocode
    Loader {
        id: mainLoader
        anchors.fill: parent
        source: "FirstPage.qml"
        Behavior on opacity {PropertyAnimation{duration:250}}
        onLoaded: secondLoader.source = ""
    }
    states:[
        State {
            name:"SecondPage"
            when: (mainCppClass.state == PanelStates.SECOND_PAGE)
        PropertyChanges {
            target: mainLoader
            source: "SecondPage.qml"
            }
        }
    ]
}

我能在网上找到的就是如何在iOS或Android上实现这一点。

如果mainRect是你的根组件,你可以在它的MouseArea中启动计时器。

 // touch area of your root component
 MouseArea {
     anchors.fill: parent
     onClicked: {
         timers.startLockTimer();
     }
     // child component
     Rectangle {
         color: "yellow"
         x: 50; y : 50
         width: 100; height: 100
     }
 }

这不会覆盖后代鼠标区域。因此,如果您触摸没有按钮/交互元素的区域,您的startLockTimer将被调用。