Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What is the most efficient way to expose a QList<int> to QML?



  • I got this question asked today and would like to know what is the answer.

    Possible answers:

    1. Exposing with Q_Property

    QT docs say this about it:

    If the sequence is exposed as a Q_PROPERTY, accessing any value in the
    sequence by index will cause the sequence data to be read from the
    QObject's property, then a read to occur. Similarly, modifying any
    value in the sequence will cause the sequence data to be read, and
    then the modification will be performed and the modified sequence will
    be written back to the QObject's property.

    1. Return QList from a function which is marked Q_INVOKEABLE

    QT docs say this about it:

    If the sequence is returned from a Q_INVOKABLE function, access and
    mutation is much cheaper, as no QObject property read or write occurs;
    instead, the C++ sequence data is accessed and modified directly.

    1. Make a signal in C++ and connect it to QML slot with a javascript function

    2. Expose the QList<int> as a Singleton in QML.

    Surely answer 2 is more efficient than answer 1 but what about the other two options?

    Interestingly I tried to ask Stackoverflow and they think it is opinion based. But I think there must be for sure the fastest way or what do you say?


Log in to reply