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

ListView model from Q_PROPERTY problems



  • I have a Q_OBJECT code:

        Q_PROPERTY(QList<QString> someStrings READ someStrings NOTIFY stringsChanged)
        QList<QString> someStrings();
    
    QList<QString> MyClass::someStrings()
    {
        QList<QString> list;
        for(int i = 0; i < 10; i++)
            list.push_back("something");
        
    
        return list;
    }
    

    and for the given QML code:

                    ListView {
                        id: myListVIew
                        model: ["one", "two"]
                        delegate:
                            Text {
                                id: text
                                text: " element "
                                font.pointSize: 20
                            }
                        Component.onCompleted:
                        {
                            console.log(myListVIew.model)
                        }
                    }
    

    I get the view of:

    element
    element

    and console log:
    qml: [one, two]

    But when i use the model from Q_OBJECT:

                    ListView {
                        id: myListVIew
                        model: my_model.someStrings
                        delegate:
                            Text {
                                id: text
                                text: " element "
                                font.pointSize: 20
                            }
                        Component.onCompleted:
                        {
                            console.log(myListVIew.model)
                        }
                    }
    

    My view is empty, nothing prints to view, but the model is filled as i get this console log:
    qml: [something,something,something,something,something,something,something,something,something,something]

    What am i doing wrong that the view is empty?



  • @Kyeiv said in ListView model from Q_PROPERTY problems:

    What am i doing wrong that the view is empty?

    The problem is that QList<QString>() is not a valid type for a QML model!

    QML model can be QList<QObject*>() or QStringList() (cf https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html).

    Change your Q_PROPERTY from QList<QString> to QStringList and it will work as expected.



  • @Kyeiv said in ListView model from Q_PROPERTY problems:

    What am i doing wrong that the view is empty?

    The problem is that QList<QString>() is not a valid type for a QML model!

    QML model can be QList<QObject*>() or QStringList() (cf https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html).

    Change your Q_PROPERTY from QList<QString> to QStringList and it will work as expected.