Parse QList<QVariantMap> to QML

  • Hello,

    With this code :


    Q_PROPERTY(QList<QVariantMap> mapList READ mapList WRITE setMapList NOTIFY mapListChanged)
    QList<QVariantMap> mapList() const;
    void setMapList(const QList<QVariantMap> &a);


    QList<QVariantMap> Folder2Xml::mapList() const
        return m_mapList;
    void Folder2Xml::setMapList(const QList<QVariantMap> &a)
        if (a != m_mapList) {
            m_mapList = a;
            emit mapListChanged();
    void Folder2Xml::getRecursive()
        QDirIterator it(m_pathDoc, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
        while (it.hasNext()) {
            QFileInfo fileInfo(;
            // m_map is a QVariantMap public member 
            m_map["fileName"] = fileInfo.baseName();
            m_map["filePath"] = fileInfo.absoluteFilePath();
        qDebug() << "m_pathDoc" << m_pathDoc;
        qDebug() << "m_mapList.length()" << m_mapList.length();

    In my case, m_mapList.length() = 219.

    But in my QML file, I can't use mapList :

            id: folder2Xml
            pathDoc: "/Path/to/my/dir/"
    function myFunction(){
            console.log(folder2Xml.mapList)  // = QVariant(QList<QVariantMap>)
            console.log(folder2Xml.mapList.length) // = 0

    Could you help me ?

    Thank you very much in advance.


  • Hi! I think you can only return certain built-in QList<Something>'s. With a quick look at the docs I think you can actually only use:

    • QList<int>
    • QList<qreal>
    • QList<bool>
    • QList<QString> and QStringList
    • QList<QUrl>
    • QVector<int>
    • QVector<qreal>
    • QVector<bool>

    and QList<QObject*>. Not even sure about QVector<QObject*>.

    For any other QList<Something> you'd use a QVariantList as your Q_PROPERTY / as return value from a Q_INVOKABLE.

  • Hi,

    Thank you for your answer. This is that I thought.
    So, I modified my code with :

    QVariant Folder2Xml::getRecursive(QString path)
        QVariantMap map;
        QVariantList vList;
        QDirIterator it(path, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
        while (it.hasNext()) {
            QFileInfo fileInfo(;
            map["fileName"] = fileInfo.baseName();
            map["filePath"] = fileInfo.absoluteFilePath();
            map["opt"] = false;
        return QVariant::fromValue(vList);

    Thank you.



Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.