首页 > 解决方案 > QML按住图像按钮以不断增加值

问题描述

我试图在按下并按住按钮时让显示值连续增加 1。我使用 Image 元素实现了我的按钮,该元素使用 MouseArea 在按下一次时增加显示值。我不确定是否有另一个支持它的默认信号,如果有,我找不到。

我正在使用Python+Pyside6,但希望这是我可以用 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"
    }

标签: qtqml

解决方案


我为你写了这个例子:如果你想为按钮设置图像,把图像放在里面。

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
    }
}

正如我从您的问题中了解到的那样: 按钮使用中有信号而不是.PressAndHoldonPressAndHoldonClicked

在此处输入图像描述


推荐阅读