Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to access QVariantList in Qml listview
Qt 6.12 LTS is out! See what's new in the release blog

How to access QVariantList in Qml listview

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 1.4k 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.
  • R Offline
    R Offline
    Raji
    wrote on last edited by
    #1

    Hi i am new Qt , any one help me how to access QVariantList in Qml listview
    I tried follwing code
    c++ class:
    {
    QJsonObject m_DataSourceObject = JsonDocument.object();
    QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("camera")).toArray();

    QVariantMap mainmap = m_DataSourceObject.toVariantMap();
    QVariantList phraseList = mainmap["camera"].toList();
    

    setTesting(phraseList);

    }

    QVariantList Jsonparsing::testing(void)
    {
    return mTesting;
    }
    void Jsonparsing::setTesting(QVariantList value)
    {
    mTesting = value;
    emit testingChanged(value);
    }

    Qml:

    property var test: jsonparsing.testing

    ListModel {
    id: model
    Component.onCompleted: {
    for(var i=0; i<5; i++){
    append({"name": test[1]});
    }
    }
    }

    ListView {
        id: list
        anchors.fill: parent
        model: model
        anchors.top: parent.top
        interactive: false
        delegate: Component {
            ColumnLayout {
                anchors.fill: parent
                TextEdit {
                    width: 93
                    height: 25
                    text: name
                    color: "red"
                    font.pixelSize: 22
                    anchors.left: parent.left
                    anchors.leftMargin: 32
                }
            }
        }
    }
    
    ODБOïO 1 Reply Last reply
    0
    • R Raji

      Hi i am new Qt , any one help me how to access QVariantList in Qml listview
      I tried follwing code
      c++ class:
      {
      QJsonObject m_DataSourceObject = JsonDocument.object();
      QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("camera")).toArray();

      QVariantMap mainmap = m_DataSourceObject.toVariantMap();
      QVariantList phraseList = mainmap["camera"].toList();
      

      setTesting(phraseList);

      }

      QVariantList Jsonparsing::testing(void)
      {
      return mTesting;
      }
      void Jsonparsing::setTesting(QVariantList value)
      {
      mTesting = value;
      emit testingChanged(value);
      }

      Qml:

      property var test: jsonparsing.testing

      ListModel {
      id: model
      Component.onCompleted: {
      for(var i=0; i<5; i++){
      append({"name": test[1]});
      }
      }
      }

      ListView {
          id: list
          anchors.fill: parent
          model: model
          anchors.top: parent.top
          interactive: false
          delegate: Component {
              ColumnLayout {
                  anchors.fill: parent
                  TextEdit {
                      width: 93
                      height: 25
                      text: name
                      color: "red"
                      font.pixelSize: 22
                      anchors.left: parent.left
                      anchors.leftMargin: 32
                  }
              }
          }
      }
      
      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      @Raji hi,
      i guess you have a Q_PROPERTY for your QVariantList mTesting , it must be directly accessible

      ListView {
          id: list
          anchors.fill: parent
          model: jsonparsing.testing
      

      @Raji said in How to access QVariantList in Qml listview:

      delegate: Component {
      ColumnLayout {
      anchors.fill: parent
      TextEdit {
      width: 93
      height: 25
      text: name
      color: "red"
      font.pixelSize: 22
      anchors.left: parent.left
      anchors.leftMargin: 32

      Don't use anchors in an item inside a Layout, you should have a warning in "Application output" tab saying that

      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
      • Unsolved