简体   繁体   中英

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

I have a QObject property declared as:

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.

In particular, QML currently supports:

  • QList< int >

...

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

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.

If I change the QML keys property to be a JS array:

property var keys: [1,2,3]

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

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?

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.

That said, I agree with you that the documentation is not clear enough since the QList seems to be a valid alternative as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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