gpt4 book ai didi

javascript - 在 QML 中通过鼠标点击推送项目时,不会调用数组的 onChanged 属性

转载 作者:行者123 更新时间:2023-11-30 17:26:18 25 4
gpt4 key购买 nike

Access.qml

import QtQuick 2.0

Rectangle {
id: newq

width: 100
height: 62

property var pp : [1]

onPpChanged:
{
console.log("\non pp changed, show pp's length: " + pp.length)
}

property int inde : 0

MouseArea
{
anchors.fill: parent
onClicked:
{
pp.push (inde++)
console.log("mousearea shows pp's length: " + pp.length)
}
}
}

main.qml

import QtQuick 2.0

Rectangle {
id: root
width: 360
height: 360

Access
{
color: "red"
}
}

输出(鼠标点击):

QML debugging is enabled. Only use this in a safe environment.

on pp changed, show pp's length: 1
mousearea shows pp's length: 2
mousearea shows pp's length: 3
mousearea shows pp's length: 4
mousearea shows pp's length: 5

当我在 pp 中推送项目时,为什么没有调用 onPpChanged?我该怎么做才能调用它?

最佳答案

这是因为 QML 中的数组不是 QML 对象。 documentation说:

Additionally, since [...] are not QML objects, changing their individual values do not trigger property change notifications. If the above example had onNumberChanged or onAnimalChanged signal handlers, they would not have been called. If, however, the items or attributes properties themselves were reassigned to different values, then such handlers would be called.

因此,为了让您的代码正常工作,您需要在修改数组时自己抛出信号。所以你的代码应该是这样的:

  MouseArea
{
anchors.fill: parent
onClicked:
{
pp.push(inde++);
ppChanged();
console.log("mousearea shows pp's length: " + pp.length);
}
}

关于javascript - 在 QML 中通过鼠标点击推送项目时,不会调用数组的 onChanged 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24177118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com