Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to use QStandardItemModel in PyQt5 for ListView to show data from a JSON file which contains a list?
Forum Update on Monday, May 27th 2025

How to use QStandardItemModel in PyQt5 for ListView to show data from a JSON file which contains a list?

Scheduled Pinned Locked Moved Unsolved Qt for Python
14 Posts 4 Posters 3.9k 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.
  • I Offline
    I Offline
    itskarantyagi
    wrote on last edited by
    #1

    I have a JSON file, I am able to fetch the data. Now, I want to display data into ListView using QStandardItemModel, but I am unable to understand how to implement. Can someone please suggest a basic implementation of how to display data so I have an idea?

    JonBJ 1 Reply Last reply
    0
    • I itskarantyagi

      I have a JSON file, I am able to fetch the data. Now, I want to display data into ListView using QStandardItemModel, but I am unable to understand how to implement. Can someone please suggest a basic implementation of how to display data so I have an idea?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @itskarantyagi
      How do you want to map/want do you want to show from the JSON to the ListView? A snippet of the JSON and what you want to display from it? If the JSON is just a list, do you want a QStandardItemModel if you're just going to display a list rather than a table, a QListView will display a list?

      I 1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        If your JSON is truly a simple list (for example, a JSON array that contains strings, numbers, and/or Booleans only), then a quick an easy way is to transfer the list data into a QStringListModel instead of QStandardItemModel.

        If your JSON contains nested structures (arrays and objects), then it's not a list but a table or tree. You will need to use QTableView or QTreeView. You can put each JSON element into a QStandardItem and put the item in a QStandardItemModel.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        I 1 Reply Last reply
        1
        • JonBJ JonB

          @itskarantyagi
          How do you want to map/want do you want to show from the JSON to the ListView? A snippet of the JSON and what you want to display from it? If the JSON is just a list, do you want a QStandardItemModel if you're just going to display a list rather than a table, a QListView will display a list?

          I Offline
          I Offline
          itskarantyagi
          wrote on last edited by
          #4

          @JonB JSON file is like this :

          [
            {
              "due": "2019-12-09",  
              "project": {
                "id": 1, 
                "name": "Volcano"
              },  
              "Assignment_name": [
                {
                  "id": 35, 
                  "name": "Alex"
                }
              ], 
              "subject": null,    
            },
            {
              "due": "2019-10-19",  
              "project": {
                "id": 2, 
                "name": "Volcanic"
              },  
              "Assignment_name": [
                {
                  "id": 36, 
                  "name": "Alexa"
                }
              ], 
              "subject": null,    
            }
            ]
          
          

          I want to query data in JSON file and display the result into a ListView, I am able to do it with QStringModel by converting the fetched details into a String, but I want to know how to do it if I want to display the list objects straight up without converting to string.

          JonBJ 1 Reply Last reply
          0
          • JKSHJ JKSH

            If your JSON is truly a simple list (for example, a JSON array that contains strings, numbers, and/or Booleans only), then a quick an easy way is to transfer the list data into a QStringListModel instead of QStandardItemModel.

            If your JSON contains nested structures (arrays and objects), then it's not a list but a table or tree. You will need to use QTableView or QTreeView. You can put each JSON element into a QStandardItem and put the item in a QStandardItemModel.

            I Offline
            I Offline
            itskarantyagi
            wrote on last edited by
            #5

            @JKSH My JSON file is like this :

            [
              {
                "due": "2019-12-09",  
                "project": {
                  "id": 1, 
                  "name": "Volcano"
                },  
                "Assignment_name": [
                  {
                    "id": 35, 
                    "name": "Alex"
                  }
                ], 
                "subject": null,    
              },
              {
                "due": "2019-10-19",  
                "project": {
                  "id": 2, 
                  "name": "Volcanic"
                },  
                "Assignment_name": [
                  {
                    "id": 36, 
                    "name": "Alexa"
                  }
                ], 
                "subject": null,    
              }
              ]
            

            I have displayed it using QStringListModel by converting every fetched data to String, but now I want to display every information from that JSON file as an object. Should I be doing it with QTableView or QTreeView?

            JKSHJ 1 Reply Last reply
            0
            • I itskarantyagi

              @JonB JSON file is like this :

              [
                {
                  "due": "2019-12-09",  
                  "project": {
                    "id": 1, 
                    "name": "Volcano"
                  },  
                  "Assignment_name": [
                    {
                      "id": 35, 
                      "name": "Alex"
                    }
                  ], 
                  "subject": null,    
                },
                {
                  "due": "2019-10-19",  
                  "project": {
                    "id": 2, 
                    "name": "Volcanic"
                  },  
                  "Assignment_name": [
                    {
                      "id": 36, 
                      "name": "Alexa"
                    }
                  ], 
                  "subject": null,    
                }
                ]
              
              

              I want to query data in JSON file and display the result into a ListView, I am able to do it with QStringModel by converting the fetched details into a String, but I want to know how to do it if I want to display the list objects straight up without converting to string.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @itskarantyagi
              This is what don't get. A QListView displays a list, i.e. a single column from a model. As you said, a QStringListModel will display a single column/value, so what did you put into the string list from the JSON? But if you want to display all that JSON data you will want multiple columns, won't you, so QTableView instead?

              Also, what are you doing on the JSON file in your code? Are you reading it into a QJsonDocument and your list is a QJsonArray, do you have your own C++ class for each row and maybe you make a QList of those, or what?

              I 1 Reply Last reply
              0
              • JonBJ JonB

                @itskarantyagi
                This is what don't get. A QListView displays a list, i.e. a single column from a model. As you said, a QStringListModel will display a single column/value, so what did you put into the string list from the JSON? But if you want to display all that JSON data you will want multiple columns, won't you, so QTableView instead?

                Also, what are you doing on the JSON file in your code? Are you reading it into a QJsonDocument and your list is a QJsonArray, do you have your own C++ class for each row and maybe you make a QList of those, or what?

                I Offline
                I Offline
                itskarantyagi
                wrote on last edited by
                #7

                @JonB I am separately querying data from JSON file, suppose: I am trying to get Project name and student ID in the mentioned JSON file, I am trying to append them to a list and returning them, Now I want to display the fetched list into my ListView. How to go about doing that?

                1 Reply Last reply
                0
                • I itskarantyagi

                  @JKSH My JSON file is like this :

                  [
                    {
                      "due": "2019-12-09",  
                      "project": {
                        "id": 1, 
                        "name": "Volcano"
                      },  
                      "Assignment_name": [
                        {
                          "id": 35, 
                          "name": "Alex"
                        }
                      ], 
                      "subject": null,    
                    },
                    {
                      "due": "2019-10-19",  
                      "project": {
                        "id": 2, 
                        "name": "Volcanic"
                      },  
                      "Assignment_name": [
                        {
                          "id": 36, 
                          "name": "Alexa"
                        }
                      ], 
                      "subject": null,    
                    }
                    ]
                  

                  I have displayed it using QStringListModel by converting every fetched data to String, but now I want to display every information from that JSON file as an object. Should I be doing it with QTableView or QTreeView?

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by JKSH
                  #8

                  @itskarantyagi said in How to use QStandardItemModel in PyQt5 for ListView to show data from a JSON file which contains a list?:

                  My JSON file is like this :

                  Your JSON document contains a Tree, not a List. QListView and QStringListModel cannot represent it properly.

                  A JSON list is more like this:

                  [
                      "A",
                      "B",
                      "C"
                  ]
                  

                  There is no straightforward way of converting your JSON tree directly into a Qt model.

                  I have implemented a JsonTreeModel in C++ that can display any arbitrary JSON document:

                  • https://github.com/JKSH/QtDataTreeModels
                  • https://jksh.github.io/QtDataTreeModels/build/API/html/class_json_tree_model.html

                  I ran the example code and pasted your JSON into it; the output is

                  JSON tree view

                  I notice that your Assignment_name is an Array. Does it always contain 1 element only, or can it have multiple elements? If only has 1 element, you could "flatten" your data into a Table like this:

                  due project.id project.name Assignment_name.id Assignment_name.name subject
                  2019-12-09 1 Volcano 35 Alex
                  2019-10-19 2 Volcanic 36 Alexa

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  I 1 Reply Last reply
                  2
                  • JKSHJ JKSH

                    @itskarantyagi said in How to use QStandardItemModel in PyQt5 for ListView to show data from a JSON file which contains a list?:

                    My JSON file is like this :

                    Your JSON document contains a Tree, not a List. QListView and QStringListModel cannot represent it properly.

                    A JSON list is more like this:

                    [
                        "A",
                        "B",
                        "C"
                    ]
                    

                    There is no straightforward way of converting your JSON tree directly into a Qt model.

                    I have implemented a JsonTreeModel in C++ that can display any arbitrary JSON document:

                    • https://github.com/JKSH/QtDataTreeModels
                    • https://jksh.github.io/QtDataTreeModels/build/API/html/class_json_tree_model.html

                    I ran the example code and pasted your JSON into it; the output is

                    JSON tree view

                    I notice that your Assignment_name is an Array. Does it always contain 1 element only, or can it have multiple elements? If only has 1 element, you could "flatten" your data into a Table like this:

                    due project.id project.name Assignment_name.id Assignment_name.name subject
                    2019-12-09 1 Volcano 35 Alex
                    2019-10-19 2 Volcanic 36 Alexa
                    I Offline
                    I Offline
                    itskarantyagi
                    wrote on last edited by
                    #9

                    @JKSH Actually, I have been given a task to query the sample JSON data I have mentioned and display using ListView only but not use QStringListModel. I am querying the JSON data and based on project id and based on project id, I am returning project name and Student name (Alex/Alexa), then I am appending them to a list and returning it. Now, I want to display the list I have got into a ListView. That's what my task is.

                    JKSHJ JonBJ 2 Replies Last reply
                    0
                    • I itskarantyagi

                      @JKSH Actually, I have been given a task to query the sample JSON data I have mentioned and display using ListView only but not use QStringListModel. I am querying the JSON data and based on project id and based on project id, I am returning project name and Student name (Alex/Alexa), then I am appending them to a list and returning it. Now, I want to display the list I have got into a ListView. That's what my task is.

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by JKSH
                      #10

                      @itskarantyagi said in How to use QStandardItemModel in PyQt5 for ListView to show data from a JSON file which contains a list?:

                      I have been given a task to query the sample JSON data I have mentioned and display using ListView only

                      OK, what do you want the view to show? How do you want the data to look like on your screen?

                      Also, who gave you this task? Your boss? Your teacher/professor? Your customer?

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      I 1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        itskarantyagi
                        wrote on last edited by itskarantyagi
                        #11

                        I want the view to display the returned list which is received when I query the JSON file to retrieve student name from project id. I am a student, I have been given this task for an internship. I have tried to go through all the possible links, and yes, the aptest solution seems to be using TreeView or TableView but it has been mentioned to use ListView only. There seem to be only a few tutorials for PyQt and even less concerning ListView.

                        JKSHJ 1 Reply Last reply
                        0
                        • JKSHJ JKSH

                          @itskarantyagi said in How to use QStandardItemModel in PyQt5 for ListView to show data from a JSON file which contains a list?:

                          I have been given a task to query the sample JSON data I have mentioned and display using ListView only

                          OK, what do you want the view to show? How do you want the data to look like on your screen?

                          Also, who gave you this task? Your boss? Your teacher/professor? Your customer?

                          I Offline
                          I Offline
                          itskarantyagi
                          wrote on last edited by
                          #12

                          @JKSH I was able to display data using QStandardItemModel just now, but somehow the integer values aren't being displayed, can you please tell why that's happening ?

                          1 Reply Last reply
                          0
                          • I itskarantyagi

                            I want the view to display the returned list which is received when I query the JSON file to retrieve student name from project id. I am a student, I have been given this task for an internship. I have tried to go through all the possible links, and yes, the aptest solution seems to be using TreeView or TableView but it has been mentioned to use ListView only. There seem to be only a few tutorials for PyQt and even less concerning ListView.

                            JKSHJ Offline
                            JKSHJ Offline
                            JKSH
                            Moderators
                            wrote on last edited by JKSH
                            #13

                            @itskarantyagi said in How to use QStandardItemModel in PyQt5 for ListView to show data from a JSON file which contains a list?:

                            I want the view to display the returned list which is received when I query the JSON file to retrieve student name from project id.

                            Please clarify your requirements:

                            1. Do you want to display all the JSON fields in the ListView? Or just the student names?
                            2. Do you want to use the C++/Python QListView or the QML ListView?

                            Most importantly: Have you managed to retrieve the student name from project id out of your JSON data? Please show your code.

                            There seem to be only a few tutorials for PyQt and even less concerning ListView.

                            Here is an official tutorial for the C++/Python QListView: https://doc.qt.io/qtforpython/overviews/model-view-programming.html -- First, you add your data to the model. Then, you put the model into the view.

                            Here is an official tutorial for the QML ListView: https://doc.qt.io/qtforpython/tutorials/qmlapp/qmlapplication.html

                            These tutorials are for "Qt for Python" (Pyside2) instead of "PyQt5", but the code should be similar.

                            I was able to display data using QStandardItemModel just now, but somehow the integer values aren't being displayed, can you please tell why that's happening ?

                            Rule #1 when asking for help to troubleshoot your code: You must show your code because we don't have a crystal ball.

                            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                            1 Reply Last reply
                            1
                            • I itskarantyagi

                              @JKSH Actually, I have been given a task to query the sample JSON data I have mentioned and display using ListView only but not use QStringListModel. I am querying the JSON data and based on project id and based on project id, I am returning project name and Student name (Alex/Alexa), then I am appending them to a list and returning it. Now, I want to display the list I have got into a ListView. That's what my task is.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #14

                              @itskarantyagi said in How to use QStandardItemModel in PyQt5 for ListView to show data from a JSON file which contains a list?:

                              based on project id and based on project id, I am returning project name and Student name (Alex/Alexa), then I am appending them to a list and returning it. Now, I want to display the list I have got into a ListView.

                              First you should answer/look at @JKSH's posts, which are very helpful.

                              Meanwhile, I keep asking you this: you are picking out 2 (two) columns/fields from the data --- project name & student name --- to display in a list, which is designed to hold single-column items. So how do you want to display these? Consecutive, alternating rows of project name as first row and student name as second row (doubtful?)? Create a string like, say, "Volcano: Alex" and use that as your list items? Just the student name given that you are filtering by one, specific project name/id and do not show the project? Or what?

                              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