sebba23
sebba23

Reputation: 574

how to change color of a rectangle when I click on it for 3 seconds

I have a rectangle. I need to activate the rectangle (change its color to red) when I click on it for 3 seconds (meaning: kept pressed for 3 seconds).

I saw the properties onPressed, onReleased and onPressAndHold but how can I use them?

Upvotes: 0

Views: 882

Answers (1)

Arman Oganesyan
Arman Oganesyan

Reputation: 426

Use Timer for that purpose.

import QtQuick 2.9
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Rectangle {
        id: rect
        anchors.fill: parent

        MouseArea {
            anchors.fill: parent

            Timer {
                id: timer
                repeat: false
                interval: 3000
                running: parent.pressed
                onTriggered: rect.state === "on" ? rect.state = "off" : rect.state = "on"
            }
        }

        states: [
            State {
                name: "on";
                PropertyChanges { target: rect; color: "green"; }
            },
            State {
                name: "off";
                PropertyChanges { target: rect; color: "red"; }
            }
        ]

        state: "on"
    }
}

Upvotes: 2

Related Questions