Pyside2 Qlistview Qtableview Sync Problem
It is python/PySide2 interface related problem, as much as I tried, I couldn't make it sync (QListView and QTableView). I will try to simplify it, instead of explaining the whole c
Solution 1:
You have to create a model with the tree structure where the dependency is seen, and in the case of the QListView it will show the root items and in the case of the QTableView it will show the leaves and it will have as rootIndex the selected QModelIndex of the QListView. For educational purposes I will show the model of the tree in a QTreeView.
from PySide2 import QtCore, QtGui, QtWidgets
dict_of_dicts={
'dict1':{'k1':'v1', 'k2':'v2', 'k3':'v3'},
'dict2':{'k4':'v4'},
'dict3':{'k5':'v5', 'k6':'v6', 'k7':'v7'},
}
defcreate_model_from_dict(d, parent=None):
model = QtGui.QStandardItemModel(0, 2, parent)
for k, v in dict_of_dicts.items():
it = QtGui.QStandardItem(k)
model.appendRow(it)
for k_, v_ in v.items():
it.appendRow([QtGui.QStandardItem(k_), QtGui.QStandardItem(v_)])
return model
classWidget(QtWidgets.QWidget):
def__init__(self, parent=None):
super(Widget, self).__init__(parent)
model = create_model_from_dict(dict_of_dicts, self)
self.tableview = QtWidgets.QTableView()
self.tableview.setModel(model)
self.listview = QtWidgets.QListView()
self.listview.setModel(model)
self.listview.selectionModel().selectionChanged.connect(self.handleSelectionChanged)
self.listview.selectionModel().select(model.index(0, 0), QtCore.QItemSelectionModel.Select)
self.treeview = QtWidgets.QTreeView()
self.treeview.setModel(model)
self.treeview.expandAll()
hlay = QtWidgets.QHBoxLayout(self)
hlay.addWidget(self.listview)
hlay.addWidget(self.tableview)
hlay.addWidget(self.treeview)
@QtCore.Slot(QtCore.QItemSelection)defhandleSelectionChanged(self, item):
ixs = item.indexes()
if ixs:
self.tableview.setRootIndex(ixs[0])
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
Post a Comment for "Pyside2 Qlistview Qtableview Sync Problem"