qt - 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"
}
解决方案
我为你写了这个例子:如果你想为按钮设置图像,把图像放在里面。
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
}
}
正如我从您的问题中了解到的那样:
按钮使用中有信号而不是.PressAndHold
onPressAndHold
onClicked
推荐阅读
- javascript - 如何从 getItem dynamoDB (JavaScript) 中检索特定对象?
- php - 返回 pdo rowCount 变量
- php - 如何将 PHP shell_exec 结果显示到 html 表中?
- javascript - WooCommerce 结帐页面中的淡出验证错误消息
- javascript - Babel 插件:如何插入兄弟节点
- sql-server - 是否可以在 SQL 中解析键:值对
- c# - “产量”返回不同的类型?
- c# - 如何让 SQL 执行提示输入查询参数?
- java - 为 Java Rest API 初始化数据的最佳方式是什么?
- reactjs - 在按钮单击上运行排序方法后更新视图