QList<int>不能用作中继器的模型
我有一个 QObject
属性声明为:
I have a QObject
property declared as:
Q_PROPERTY( QList< int > keys READ getKeys NOTIFY keysChanged )
在 docs 声明如下:
某些 C++ 序列类型在 QML 中被透明地支持为JavaScript 数组类型.
Certain C++ sequence types are supported transparently in QML as JavaScript Array types.
具体来说,QML 目前支持:
In particular, QML currently supports:
- QList<诠释 >
...
但是,当我使用此属性来驱动 Repeater
模型时:
However, when I use this property to drive a Repeater
model:
QtObject {
id: d_
property var keys: base.proxy.keys // A binding to the C++ keys property
onKeysChanged: {
...
}
}
Column {
spacing: 4
Repeater {
id: repeater
model: d_.keys
delegate: Rectangle {
height: 24
width: 24
color: "red"
}
}
}
Repeater
模型不产生委托.如果我查询 d_.keys
的长度,它会显示正确的数量,如果我从 C++ 更改属性,则会触发 d_.onKeyChanged:{}
- 但Repeater
从不构建任何东西.
The Repeater
model produces no delegates. If I query the length of d_.keys
, it shows the correct quantity, and if I change the property from C++, d_.onKeyChanged:{}
is triggered ― but the Repeater
never builds anything.
如果我将 QML keys
属性更改为 JS 数组:
If I change the QML keys
property to be a JS array:
property var keys: [1,2,3]
Repeater
按预期工作.如果我使用 C++ 属性,但手动将数据转换为 JS 数组,它也可以按预期工作:
The Repeater
works as expected. If I use the C++ property, but manually convert the data to a JS array, it also works as expected:
QtObject {
id: d_
property var keys: base.proxy.keys
onKeysChanged: {
var list = [];
for ( var i = 0; i < keys.length; ++i ) {
list.push( keys[i] );
}
repeater.model = list;
}
}
这强烈表明,不管文档怎么说,QList<int>
不 等同于 JS 数组.是我做错了什么,还是这是一个错误?
This strongly indicates that despite what the docs say, QList<int>
is not equivalent to a JS array. Am I doing something wrong, or is this a bug?
推荐答案
如上所述 here,QVariantList
被转换为JS数组,因此问题可能出在内容的类型上不是列表本身.
As described here, the QVariantList
is converted to a JS array, therefore the problem may be the type of the content not the list itself.
也就是说,我同意你的观点,即文档不够清晰,因为 QList
似乎也是一个有效的替代方案.
That said, I agree with you that the documentation is not clear enough since the QList
seems to be a valid alternative as well.
相关文章