Printing a QMap with a ListView
-
Hello,
I'm trying to display a QMap<int, QString> that I have on the C++ side in a ListView present on the QML side. I cannot turn it in a QStringList because I will need the keys of QMap later on this Listview.
I'm not sure how to proceed, but after some research I saw that I had to use a QVariantMap to pass my QMap to my QML:C++ Code (_userList is a QMap<int, QString> previously filled):
QVariantMap User::usersMap() { QVariantMap myVariantMap; QMapIterator<int, QString> j(_usersList); while(j.hasNext()) { j.next(); myVariantMap[QString::number(j.key())] = j.value(); } return myVariantMap; }QML :
ListView { id: userListView spacing: 5 anchors.fill: parent model: user.usersMap() delegate: RowLayout { anchors.left: parent.left anchors.leftMargin: 25 spacing: 20 Text { text: modelData anchors.left: parent.left anchors.leftMargin: 75 font.pixelSize: 28 color: "white" } } }My biggest doubt is about the initialization of the "model" field in QML and accessing it via modelData. If you have any clues or advice on how to display this QMap<int, QString>, I thank you in advance.
-
Hello,
I'm trying to display a QMap<int, QString> that I have on the C++ side in a ListView present on the QML side. I cannot turn it in a QStringList because I will need the keys of QMap later on this Listview.
I'm not sure how to proceed, but after some research I saw that I had to use a QVariantMap to pass my QMap to my QML:C++ Code (_userList is a QMap<int, QString> previously filled):
QVariantMap User::usersMap() { QVariantMap myVariantMap; QMapIterator<int, QString> j(_usersList); while(j.hasNext()) { j.next(); myVariantMap[QString::number(j.key())] = j.value(); } return myVariantMap; }QML :
ListView { id: userListView spacing: 5 anchors.fill: parent model: user.usersMap() delegate: RowLayout { anchors.left: parent.left anchors.leftMargin: 25 spacing: 20 Text { text: modelData anchors.left: parent.left anchors.leftMargin: 75 font.pixelSize: 28 color: "white" } } }My biggest doubt is about the initialization of the "model" field in QML and accessing it via modelData. If you have any clues or advice on how to display this QMap<int, QString>, I thank you in advance.
You can try something like this.
ListView { id: userListView spacing: 5 anchors.fill: parent property var maps : user.usersMap(); property var keyss : Object.keys(user.usersMap()) model: keyss delegate:Text { text: modelData + " Value = "+userListView.maps[modelData] anchors.leftMargin: 75 font.pixelSize: 28 } }