Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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


  • Banned

    I am going through that headache myself and yeah that data and examples on that are not all that straight foward still I have found some documentation and examples and I am putting together a more comprehensive bit a personal documentation on this so that once I have it fully figured out I will have a solid bit of documentation to refer to as well as share with others.

    In the mean time if you want to collaborate on this some (share knowledge) you can shoot me a message on Discord DenniO#8137 and we can kibitz some.



  • @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?


  • Moderators

    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.



  • @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.



  • @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?



  • @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?



  • @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?


  • Moderators

    @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:

    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


  • @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.


  • Moderators

    @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 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.



  • @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 ?


  • Moderators

    @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.



  • @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?


  • Banned

    Okay if you are using the correct base Model type all you are needing then is to bring your data into to an object -- be it a defined class you made or some predefined object that handles storing the data within the program

    Once the data is stored within the program in some manner then the Model can use used to define how the data is to be rendered to the user -- it is the C-V of M-C-V (or Model Controller View or more commonly called MVC for some strange reason). Yes its called a Model but it really is not per their own documentation which makes it a bit more confusing. It just takes your Data Source (Model) and presents it to the View (Gui) so in my book that is the Controller however it is tied fairly tightly to the Gui which is why they say its sort of a combined Controller-View.

    So with that understanding once you have the data in some internal object -- you give access to the object to your AbstractModel and it selects the data elements it is going to present to the View (Gui) and how those data elements are to be viewed (Bolded, Centered, etc...) Further you can have more than just one of these Models operating off the same Source Data that are used to present different aspects of the data.

    Now if you need to edit that data that you are displaying then you are supposed to use a Delegate I hope that helps some again drop me a line on Discord DenniO#8137 if you like


Log in to reply