QML 绑定到数组元素 [英] QML binding to an array element

查看:34
本文介绍了QML 绑定到数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 QML Rectangle 上有一个 width 属性,该属性是基于另一个 Rectangle 设置的,ID 为 mainwindow 和一个数组mainwindow 的属性:

I have a width property on a QML Rectangle that is set based on another Rectangle with an id of mainwindow and one of the array properties of mainwindow:

width: mainwindow.width/mainwindow.numColsPerRow[positionRow]

这在我的矩形设置时有效;也就是数组numColsPerRow里面的元素是正确涉及的.

This works at the time my rectangle is setup; that is, the element inside the array numColsPerRow is correctly involved.

但是,在这个矩形设置之后,如果我改变numColsPerRow里面的值,这个矩形的width没有任何影响.

However, after this Rectangle is setup, if I change the values inside numColsPerRow the width of this Rectangle does not have any effect.

QML 是否不允许将属性绑定到数组元素?

Does QML not allow property bindings to array elements?

推荐答案

var JS 数组中的值在调用时不会发出和已更改"信号:

Values in a var JS array don't emit and 'changed' signal when you call :

my_array  [n] = value;

为了将数组属性通知给使用它的每个代码,您必须使用这个技巧:

In order to get the array property notified to every code using it you must use this trick :

var tmp =  my_array;
tmp [n] = value; // you can do multiple changes, and also push/splice items
my_array = tmp;

这样,QML 引擎将发出信号,其他使用 my_array 的绑定将得到通知和更新.

This way, QML engine will emit the signal and other bindings using my_array will be notified and updated.

PS:您不能为此使用 ListModel,因为您无法使用数组或映射等键来获取模型中的特定项目.模型旨在与 MVC 视图一起使用...

PS: you can't use a ListModel for this, because you won't have a way to get a particular item in the model using a key like array or map do. Models are meant to be used with a MVC view...

这篇关于QML 绑定到数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆