Unsolved How to use QStandardItemModel in PyQt5 for ListView to show data from a JSON file which contains a list?
-
@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. AQListView
displays a list, i.e. a single column from a model. As you said, aQStringListModel
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, soQTableView
instead?Also, what are you doing on the JSON file in your code? Are you reading it into a
QJsonDocument
and your list is aQJsonArray
, do you have your own C++ class for each row and maybe you make aQList
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?
-
@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
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.
-
@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 ?
-
@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:
- Do you want to display all the JSON fields in the ListView? Or just the student names?
- Do you want to use the C++/Python
QListView
or the QMLListView
?
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.htmlThese 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?