Domen Kern
Domen Kern

Reputation: 138

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

I am writing an application for an embedded linux touchscreen device using Qt and Qml.
I need to implement a lock screen that appears after 30 seconds of inactivity.
To do this I've implemented a timer in C++, that changes the state of the program after timeout.

Is it possible to refresh this timer on a global scale every time the user touches the screen, so that I do not have to call the timers' start() slot in every touchable element in my program?

I would also like to refresh this timer even when the user touches a part of the screen where there are no buttons/interactive elements.

Something like this 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"
            }
        }
    ]
}

All I was able to find on the web was how to implement this in either iOS or Android.

Upvotes: 4

Views: 1840

Answers (1)

sam
sam

Reputation: 2820

If mainRect is your root component, you can just start your timer in its 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
     }
 }

This does not override descendant MouseAreas. Thus, if you touch area where there are no buttons/interactive elements, your startLockTimer will be called.

Upvotes: 1

Related Questions