Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Parse QList<QVariantMap> to QML

Parse QList<QVariantMap> to QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CharlieGC Offline
    CharlieGC Offline
    CharlieG
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      3
      • CharlieGC Offline
        CharlieGC Offline
        CharlieG
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved