Note: I am a complete beginner.
I am using a pandas dataframe to import a csv file that I converted from the following table. I need to load the csv file into a QTreeView and have been unable to do so.
The output format should be:
I will have to retrieve the "Scale Type" at some point, so I would like to somehow tag it to that item, but I'm not sure if that is possible. I have tried converting my data into a pandas dataframe and loading; loading the data directly with a csv; and converting it into a dictionary with no luck.
I can hard-code that data in, but I would prefer to use a csv as I can easily change it later.
Currently using:
model.setHeaderData(0,Qt.Horizontal,"Category")
model.setHeaderData(1,Qt.Horizontal,"Sub Category")
model.setHeaderData(2,Qt.Horizontal,"Test")
self.ui.tv.setModel(model)
self.SetContent(model)
def SetContent(self, model):
self.ui.tv.setModel(model)
i=0
for k,featuretype in features.items():
parent1 = QStandardItem('{}'.format(k[1]))
for item in featuretype:
child = QStandardItem(item[0])
if len(item[1])>0:
for listitem in item[1]:
gchild=QStandardItem(listitem)
child.appendRow(gchild)
parent1.appendRow(child)
model.setItem(i,0,parent1)
self.ui.tv.setFirstColumnSpanned(i,self.ui.tv.rootIndex(),True)
i+=1
This only works when values are hard keyed like:
features = {('POLYGON', 'SLPR'): [('ONE WAY', ['NO', 'YES','maybe'], 'List', 3), ('CLASS', ['INTERSTATE', 'PRIMARY', 'RESIDENTIAL', 'SECONDARY', 'SERVICE', 'STATE HWY', 'TERTIARY', 'TRACK', 'US HWY'], 'List', 11)]
But it doesn't work with dictionary objects I create from the csv or dataframe, and I get the "String Index out of Range Error".
I also found this code, which would have been great. But it gives me duplicates and the parents only.
reader = csv.reader(f)
for row in reader:
item = QTreeWidgetItem(self.ui.tv, row)
print(row)