Parse QList<QVariantMap> to QML
Solved
QML and Qt Quick
-
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
-
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>
andQStringList
QList<QUrl>
QVector<int>
QVector<qreal>
QVector<bool>
and
QList<QObject*>
. Not even sure aboutQVector<QObject*>
.For any other
QList<Something>
you'd use aQVariantList
as yourQ_PROPERTY
/ as return value from aQ_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