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

Automatic conversion of QList<QString> is not a JS array of strings?



  • Hi.
    I wish to set the model of a ListView to a QList<QString> passed from C++ via a Q_INVOKABLE function call.

    According to the Qt docs, the conversion from QList<QString> to a JavaScript array is automatic.

    Yet, the view does not show the data. If I set the model directly in QML to a JS string array with exactly the same strings however it works.
    Converting it explictly to a JS array with Array.from() also works.

        property var names: []
    
        Component.onCompleted: {
            names = um.getNames()
    
            //console.debug(names) //<-- shows [Name1,Name2]
    
            //view.model = ["Name1", "Name2"]      //<-- WORKS
            //view.model = [names[0], names[1]]   //<-- WORKS
            //view.model = Array.from(names)         //<-- WORKS
    
            view.model = names // <-- DOES NOT WORK ??
       }
    

    What am I missing here? Is the implicit conversion done by the QML engine of a QList<QString> not a normal JS array of strings?


  • Qt Champions 2018

    Have you tried with QStringList?



  • @GrecKo
    QStringList works without conversion to Array. So only the QList<QString> conversion is not transparent.

    Thanks.


Log in to reply