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

[solved] QQmlListProperty from QList



  • All the examples I've found online say to do what I've done in the following example, however I get an error.

    @QQmlListProperty<QString> ReadXML::listElements()
    {
    QList<QString> eleList;
    eleList.append("one");
    eleList.append("two");
    eleList.append("three");

    return QQmlListProperty<QString>(this, eleList);
    

    }@

    @readxml.cpp:14: error: C2665: 'QQmlListProperty<QString>::QQmlListProperty' : none of the 5 overloads could convert all the argument types
    d:\qt\qt5.3.2\5.3\msvc2013_opengl\include\qtqml\qqmllist.h(67): could be 'QQmlListProperty<QString>::QQmlListProperty(QObject *,QList<T *> &)'
    with
    [
    T=QString
    ]
    while trying to match the argument list '(ReadXML *const , QList<QString>)'@

    Due to the lack of information given by the compiler I don't know how to troubleshoot it. It sounds as if it is telling me to do this:

    @return QQmlListProperty<QString>(this, eleList<QString>);@

    But that just results in it complaining:
    @readxml.cpp:14: error: C2275: 'QString' : illegal use of this type as an expression@

    I would appreciate any help.



  • From the declaration QQmlListProperty(QObject ,QList<T > &) requires a list of the pointers to some type.
    You are providing a list of types.
    Try this
    @
    QQmlListProperty<QString> ReadXML::listElements()
    {
    QList<QString
    > eleList;
    QStringList sourceList;
    source << "one" << "two" << "three";
    for(QString source : sourceList) {
    QString
    string = new QString(source);
    eleList.append(string);
    }
    return QQmlListProperty<QString>(this, eleList);
    }
    @



  • But are you trying to pass a list of QString ?!?! If yes, then QStringList is already QML friendly. So, you don't need to create a QQmlListProperty for containing a list of QString, just return a QStringList from the slot:

    @
    QStringList ReadXML::listElements() {
    QStringList eleList;
    eleList.append("one");
    eleList.append("two");
    eleList.append("three");
    return eleList;
    }
    @



  • Thank you both.

    Gianluca - good to know, and andreyc's is good reference for future requirements.

    Cheers


Log in to reply