QtreeView only able to sort by first and second column. Ia want to be able to sort by any column
-
I have a Qtreeview as follow in the image:
it is filled from nested dictionnaries such as:
{well:{depthtype:{logtype:[log1,log2,log3]}}} all key and list values are strings
I can sort by well name, by depth type, but not by log type or log name
the model is a QstandardItemModel
I add the data in the model with the following function:
def fillWellTree(self, parent, dico, depth=0): if isinstance(dico, dict): for key, value in dico.items(): item1=QStandardItem(str(key)) item1.setEditable(False) itemList=[item1] if depth==0: itemList[0].setIcon(QIcon("./icon/IconWell.png")) if isinstance(value, dict): for i in range(depth): emptyItem=QStandardItem("") emptyItem.setEditable(False) itemList.insert(0,emptyItem) parent.appendRow(itemList) self.fillWellTree(itemList[0], value, depth+1) elif isinstance(value, list): for i in range(depth): emptyItem=QStandardItem("") emptyItem.setEditable(False) itemList.insert(0,emptyItem) parent.appendRow(itemList) for val in value: item_i=QStandardItem(str(val)) item_i.setEditable(False) itemLogList=[item_i] for i in range(depth+1): emptyItem=QStandardItem("") emptyItem.setEditable(False) itemLogList.insert(0,emptyItem) itemList[0].appendRow(itemLogList)
I do not quite understand why only the 2 first columns are sortable.
Thank you for your help
-
Hi and welcome to devnet,
Which version of PyQt / PySide are you using ?
On which OS ?
Can you provide a minimal runnable script that shows this behavior ? -
Hi
Thank you. I am using PySide6 on windows
here is a runnable example that demonstrate the problemimport sys from PySide6.QtWidgets import QApplication, QTreeView from PySide6.QtGui import QStandardItem, QStandardItemModel app = QApplication(sys.argv) TreeViewLogSelection=QTreeView() WellModel = QStandardItemModel() WellModel.setColumnCount(6) TreeViewLogSelection.setModel(WellModel) TreeViewLogSelection.model().setHorizontalHeaderLabels(['Well Name','Depth type','Log Type','Log Name','Role','Log unique name']) def fillWellTree(parent, dico, depth=0): if isinstance(dico, dict): for key, value in dico.items(): item1=QStandardItem(str(key)) item1.setEditable(False) itemList=[item1] if isinstance(value, dict): for i in range(depth): emptyItem=QStandardItem("") emptyItem.setEditable(False) itemList.insert(0,emptyItem) parent.appendRow(itemList) fillWellTree(itemList[0], value, depth+1) elif isinstance(value, list): for i in range(depth): emptyItem=QStandardItem("") emptyItem.setEditable(False) itemList.insert(0,emptyItem) parent.appendRow(itemList) for val in value: item_i=QStandardItem(str(val)) item_i.setEditable(False) itemLogList=[item_i] for i in range(depth+1): emptyItem=QStandardItem("") emptyItem.setEditable(False) itemLogList.insert(0,emptyItem) itemList[0].appendRow(itemLogList) TreeViewLogSelection.setSortingEnabled(True) dico={'Well-6': {'Depth': {'Sonic': ['DT', 'DTS', 'DTST', 'VPVS', 'DT_REG', 'Smoothing (DT_REG)', 'DT_FINAL'], 'Shallow resistivity': ['LLS', 'MSFL'], 'Bulk density': ['RHOB_RAW', 'RHOB_Predict_RFA', 'RHOB_REG', 'RHOB_DESPIKED', 'RHOB_DESPIKE_REG', 'Smoothing (RHOB_DESPIKE_REG)', 'RHOB_FINAL', 'RHOB_Predict_RF'], 'Shear slowness': ['DTS_Predict_RF', 'DTS_Predict_NN'], 'Deviation': ['DX', 'DY']}, 'MD': {'Sonic': ['DT_Predict_RF','DTS_predict']}}, 'DRILL-1': {'Depth': {'Bit size': ['BS'], 'Caliper': ['CALI'], 'Gamma ray': ['GR'], 'Neutron': ['NPHI'], 'Photoelectric factor': ['PE'], 'Spontaneous potential': ['SP'], 'Shallow resistivity': ['LLS'], 'Deep resistivity': ['LLD'], 'Sonic': ['DT', 'DTS','DTC'], 'Bulk density': ['RHOB'], 'Anonymous': ['WELLTOPS'], 'Badhole indicator': ['Bad_Hole']}}, 'WELLI-1': {'Depth': {'Sonic': ['DT', 'DTS','DTC'], 'Gamma ray': ['GR', 'GR2'], 'Shallow resistivity': ['LLS', 'MSFL'], 'Bulk density': ['RHOB_RAW', 'RHOB_Predict_RF']}, 'MD': {'Sonic': ['DT_Predict_RF', 'DT_Merged', 'DT_FINAL'], 'Impedance': ['AI','IP']}}} fillWellTree(WellModel.invisibleRootItem(),dico) TreeViewLogSelection.show() sys.exit(app.exec())
Thank you for your help
-
Hi and welcome to devnet,
Which version of PyQt / PySide are you using ?
On which OS ?
Can you provide a minimal runnable script that shows this behavior ? -
@SGaist Hi thank you very much for your help
I just added a running code in the thread (just forgot to answer directly to you)
I am using Pyside6 on windows -
@babar said in QtreeView only able to sort by first and second column. Ia want to be able to sort by any column:
I am using Pyside6 on windows
Let him know the exact version of PySide6/Qt6 you are using, i.e. 6.x what "x"?
-
I currently don't know why it does not work for these columns but in between, what you can do is use a QSortFilterProxyModel.
-
I got my answer : this issue is due to the way QStandardItemModel sorts the column, it only works if the parent item has valid item in the same column. The way I built the tree does not provide an item for all the columns of each appended row so I ended up with this issue.
solution1 create these empty items
solution 2 use a QSortFilterProxyModel
Anyway a big thank you for your support attention and help.