Orange Macaron
Orange Macaron

Reputation: 53

QML Hold Image Button to continuously increment value

I am trying to have a display value continuously increment by 1 while a button is pressed and held down. I implemented my button with an Image element which is using a MouseArea to increment the display value when pressed once. I am not sure if there is another default signal that supports it, and if there is, I can't find one.

I am using Python+Pyside6, but would hope this is something I can accomplish with just QML.

   Image{
        id: incrementValue
        source: "icons/arrow-up-circle-sharp.svg"
        fillMode: Image.PreserveAspectFit
        property int size: 50
        sourceSize.width: size
        sourceSize.height: size
        smooth: true
        Layout.alignment: Qt.AlignTop
        visible: true

        MouseArea {
            anchors.fill: parent
            onClicked: {
                displayValue.text = (( displayValue.text*1.00) + 1.0).toLocaleString(Qt.locale("f"))
            }
        }
    }

    Text{
        id: displayValue
        text: "00.00"
    }

Upvotes: 0

Views: 786

Answers (2)

André
André

Reputation: 590

Use a Timer with a 1s interval. Bind it’s running property to the pressed property of a MouseArea or Button. On timeout, increase respectively decrease the value.

Upvotes: 1

Parisa.H.R
Parisa.H.R

Reputation: 3893

I write this Example For you : If you want set Image for Button Put Image inside it.

import QtQuick 2.12
import QtQuick.Window 2.12
import Qt.labs.qmlmodels 1.0
import QtQuick.Controls 2.13

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

    property int counter: 0

    Button {
        id: button
        x: 45
        y: 46
        text: qsTr("up")
        onClicked:
        {
            counter +=1

        }
    }

    Button {
        id: button1
        x: 188
        y: 46
        text: qsTr("down")
        onClicked:
        {
            counter -=1

        }
    }

    Text {
        id: name
        x: 118
        y: 130
        width: 98
        height: 60
        text: counter
    }
}

As I understand from your question : There is PressAndHold Signal in Button use onPressAndHold instead of onClicked.

enter image description here

Upvotes: 1

Related Questions