我建了一棵树,如图所示。
我希望树能够对每个列进行排序,这对“井名”列和“深度类型”列有用(我可能有不同的深度类型,称为“深度”、“MD”、“时间”等),但它对任何其他列都不起作用。据我所知,我为每列使用的项目类型没有区别:一个由字符串构建的简单 Qstandard 项目。
该树由嵌套字典和列表填充,形式如下:{well:{depthtype:{logtype:[log1,log2,log3]}}}
使用以下代码
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)
我想知道排序是否不起作用,因为最后 2 个填充列(日志类型和日志名称)中包含数据的行的父项有空字符串,所以我用随机字符串替换了空字符串,但它没有改变任何东西谢谢你的帮助和建议
这里编辑是一个工作脚本:
import 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())