Timer running on mouseArea.pressed

  • I had the idea of making a button that only if held for some time it would unlock its functionality. If pressed and released immediately nothing would happen.

    I am at the point where I created my button (a cute circular one) that when pressed animates to a scale 1.2 times bigger its original size and the text changes from 'Press and Hold' to 'Hold'.

    I also placed an arc around this button, that will act as a circular loader and will reach 100% at the time the button is finally ready to be released.

    What I am not sure how is done though is having a function with a timer that kicks in as long as the button is pressed and after the required time (lets say 2s) has passed will emit a signal or do whatever else the button is supposed to do.

    Any tips would be welcomed.

  • Hi,

    soon (in Qt 5.5) you'll have this.

    BTW, you should have a timer that

    • is started when the button is pressed
    • is stopped when the button is released
    • expires after the interval

    so you can catch the timeout signal and execute your function

  • Hi mcosta, weird thats exactly what I am making. I had no idea it would be included in 5.5

    The thing is that not only I want my timer to expire after the lets say 2 seconds, but I also want it to update my circular loader in the meantime. And I don't know how to do that.

  • I moved a little bit using the following

    Timer {
            id: elapsedTimer
            interval: 10; running: false; repeat: false
            onTriggered: canvas.timeChanged()
    function timeChanged() {
            canvas.currentValue = canvas.currentValue + 1
            if(canvas.currentValue >= 100) {
                // here I must emit a signal

    Now I will enter inside the if condition at the desired time. But what should I do once I am in there?
    Can I somehow emit a signal so whoever is using the button can attach to it?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.