Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 697
cmannett85 Avatar asked Sep 27 '22 01:09

cmannett85


1 Answers

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.

like image 51
skypjack Avatar answered Nov 15 '22 05:11

skypjack