I can show a list of items in the QListWidget, and on click get the item selected. What I can't seem to figure out how to drill down the data (I don't want to use tree view).
So as an example, the first list could be list of TV Shows. Clicking a show shows list of Seasons. Clicking a season shows the list of episodes.
I can clear and add items back to the tree, but I want to retain the ability to go back to previous list.
Would hide the top list, create new list, and add a back item element clicking which deletes the top level and shows the previous list?
Instead of recreating the information of a QListWidget you can use a model with a tree-like structure with a QListView and change the rootIndex of the view:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
data = {
"item1": {
"item 1-1": ["item 1-1-1", "item 1-1-2", "item 1-1-3"],
"item 1-2": ["item 1-2-1", "item 1-2-2", "item 1-2-3"],
"item 1-3": ["item 1-3-1", "item 1-3-2", "item 1-3-3"],
},
"item2": {
"item 2-1": ["item 2-1-1", "item 2-1-2", "item 2-1-3"],
"item 2-2": ["item 2-2-1", "item 2-2-2", "item 2-2-3"],
"item 2-3": ["item 2-3-1", "item 2-3-2", "item 2-3-3"],
},
"item3": {
"item 3-1": ["item 3-1-1", "item 3-1-2", "item 3-1-3"],
"item 3-2": ["item 3-2-1", "item 3-2-2", "item 3-2-3"],
"item 3-3": ["item 3-3-1", "item 3-3-2", "item 3-3-3"],
},
}
# https://stackoverflow.com/a/53747062
def fill_model_from_json(parent, d):
if isinstance(d, dict):
for k, v in d.items():
child = QtGui.QStandardItem(str(k))
parent.appendRow(child)
fill_model_from_json(child, v)
elif isinstance(d, list):
for v in d:
fill_model_from_json(parent, v)
else:
parent.appendRow(QtGui.QStandardItem(str(d)))
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.view = QtWidgets.QListView()
self.button = QtWidgets.QPushButton("Up")
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.view)
lay.addWidget(self.button)
self.button.setEnabled(False)
self.view.clicked.connect(self.handle_view_clicked)
self.button.clicked.connect(self.handle_button_clicked)
model = QtGui.QStandardItemModel()
self.view.setModel(model)
fill_model_from_json(model.invisibleRootItem(), data)
@QtCore.pyqtSlot(QtCore.QModelIndex)
def handle_view_clicked(self, index):
if self.view.model().hasChildren(index):
self.view.setRootIndex(index)
self.button.setEnabled(self.view.rootIndex().isValid())
@QtCore.pyqtSlot()
def handle_button_clicked(self):
self.view.setRootIndex(self.view.rootIndex().parent())
self.button.setEnabled(self.view.rootIndex().isValid())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())