简体   繁体   English

的QList <int> 不能用作Repeater的模型

[英]QList<int> cannot be used as a model for Repeater

I have a QObject property declared as: 我有一个QObject属性声明为:

Q_PROPERTY( QList< int > keys READ getKeys NOTIFY keysChanged )

And in the docs it is stated that: 文档中说明:

Certain C++ sequence types are supported transparently in QML as JavaScript Array types. 在QML中透明地支持某些C ++序列类型作为JavaScript数组类型。

In particular, QML currently supports: 特别是,QML目前支持:

  • QList< int > QList <int>

... ...

However, when I use this property to drive a Repeater model: 但是,当我使用此属性来驱动Repeater模型时:

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"
        }
    }
}

The Repeater model produces no delegates. Repeater模型不产生代理。 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. 如果我查询d_.keys的长度,它会显示正确的数量,如果我从C ++更改属性, d_.onKeyChanged:{}触发d_.onKeyChanged:{} - 但Repeater永远不会构建任何内容。

If I change the QML keys property to be a JS array: 如果我将QML keys属性更改为JS数组:

property var keys: [1,2,3]

The Repeater works as expected. Repeater按预期工作。 If I use the C++ property, but manually convert the data to a JS array, it also works as expected: 如果我使用C ++属性,但手动将数据转换为JS数组,它也可以按预期工作:

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;
    }
}

This strongly indicates that despite what the docs say, QList<int> is not equivalent to a JS array. 这强烈表明,尽管文档说的是, QList<int> 并不等同于JS数组。 Am I doing something wrong, or is this a bug? 我做错了什么,或者这是一个错误?

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. 如所描述的在这里 ,所述QVariantList转换为JS阵列,因此该问题可以是内容的类型不列表本身。

That said, I agree with you that the documentation is not clear enough since the QList seems to be a valid alternative as well. 也就是说,我同意你的观点,文档不够清晰,因为QList似乎也是一个有效的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM