Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Highlighted
Developer
Posts: 38
Registered: ‎01-13-2013
My Device: BlackBerry Z10, Q10
My Carrier: Claro (Brazil)
Accepted Solution

QML: calculate time interval between touch events

I'm creating a custom button which is activated by a touch event, this way:

 

Container {
    property bool pressed
    signal clicked
... onTouch: { if (enabled && ! event.isMove()) { if (event.isUp() && pressed) clicked() pressed = event.isDown() } } }

 

The problem with this approach is that if the user hits the custom button several times in a short period the click signal is triggered several times too. This does not happen if I use an ordinary Button or ImageButton.

 

I was thinking about getting the event timestamp and fire the clicked event only if the touch appened after at least x milliseconds since the previous Up event. The proble is that I did not find a way to calculate this difference in QML.

 

Any suggestions?

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: QML: calculate time interval between touch events

Use a QTimer to set a boolean value.

 

Once the first touch has been called set the boolean variable to false (say boolean bTouchAllowed) then wrap an if statement around the onTouch that only gets executed if bTouchAllowed is true.

 

Now on the first touch start a QTimer for the length you want, say half a second, that sets that boolean value back to true.

 

Alternatively use a gesturehandler.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Retired
Posts: 418
Registered: ‎07-18-2012
My Device: Q10
My Carrier: Bell Canada

Re: QML: calculate time interval between touch events

You could also track the last event's time with the Javascript date.

 

lastTouched = new Date().valueOf();

 then - check if enough time has passed...

--
Rob is no longer associated with BlackBerry.
Developer
Posts: 38
Registered: ‎01-13-2013
My Device: BlackBerry Z10, Q10
My Carrier: Claro (Brazil)

Re: QML: calculate time interval between touch events

Solved! All I had to do was this:

 

Container {
...
    property bool pressed
    property double lastClickTime: 0 // does not fit on an int property

    onTouch: {
        if (enabled && ! event.isMove()) {
            if (event.isUp() && pressed) {
                var now = new Date().valueOf()
                if (now - lastClickTime > 500) clicked()
                lastClickTime = now
            }
            pressed = event.isDown()
        }
    }
}

 So now it ignores continuous clicks not separated by an interval of at least 500 milliseconds.