Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

qt - QML binding to an array element

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]

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

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

Does QML not allow property bindings to array elements?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...