Parse QList<QVariantMap> to QML



  • Hello,

    With this code :

    Folder2Xml.h

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

    Folder2Xml.cpp

    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()
    {
        m_mapList.clear();
        QDirIterator it(m_pathDoc, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
        while (it.hasNext()) {
            QFileInfo fileInfo(it.next());
            // m_map is a QVariantMap public member 
            m_map["fileName"] = fileInfo.baseName();
            m_map["filePath"] = fileInfo.absoluteFilePath();
    
            m_mapList.append(m_map);
        }
    
        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 :

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

    Could you help me ?

    Thank you very much in advance.

    Charlie


  • Moderators

    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(it.next());
    
            map["fileName"] = fileInfo.baseName();
            map["filePath"] = fileInfo.absoluteFilePath();
            map["opt"] = false;
    
            vList.append(map);
            map.clear();
        }
    
        return QVariant::fromValue(vList);
    }
    

    Thank you.

    Bye

    -C


Log in to reply
 

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