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. Printing a QMap with a ListView
Qt 6.11 is out! See what's new in the release blog

Printing a QMap with a ListView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 694 Views
  • 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.
  • P Offline
    P Offline
    Pantoufle
    wrote on last edited by Pantoufle
    #1

    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.

    dheerendraD 1 Reply Last reply
    0
    • P Pantoufle

      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.

      dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      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
          }
      }
      

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      1 Reply Last reply
      0

      • Login

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