modified Qt example editabletreemodel
-
Hello,
I am stuck with
QTreeView
. I don't get it how it works.Following that offical example for the implementation of a
QTreeView
(Editable Tree Model Example) I have slightly modified it.The only change is in the constructor of TreeModel. I commented out the read of the file
default.txt
and add some items by handWindow:
self.model = TreeModel(["eins" ,"zwei"], "drei") self.ui.treeView.setModel(self.model)
TreeModel:
class TreeModel(QAbstractItemModel): def __init__(self, headers: list, data: str, parent=None): super().__init__(parent) self.root_data = headers self.root_item = TreeItem(self.root_data.copy()) # self.setup_model_data(data.split("\n"), self.root_item) i1 = TreeItem(["1", "2"]) i11 = TreeItem(["11", "12"]) i1.child_items.insert(0, i11) i12 = TreeItem(["21", "22"]) i1.child_items.insert(1, i12) self.root_item.child_items.insert(0, i1) qDebug(self.__repr__())
Tree structure (i.e.
qDebug(self.__repr__())
):<treeitem.TreeItem at 0x7f458590b320 "eins" "zwei", 1 children> <treeitem.TreeItem at 0x7f458590b8f0 "1" "2", 2 children> <treeitem.TreeItem at 0x7f458590b920 "11" "12", 0 children> <treeitem.TreeItem at 0x7f458590b950 "21" "22", 0 children>
View:
You can easily see, that the data tree is correct but the view is showing some weird result.
What's wrong here? I do not find the mistake. And again, the original example is working and my small modification is braking it.
J.
-
@MasterQ said in modified Qt example editabletreemodel:
self.model = ProjectsTreeModel(["eins" ,"zwei"], "drei")
Where is
ProjectsTreeModel
suddenly coming from?!
Your implementation is calledTreeModel
, but you seem to create another model class.Also:
ProjectsTreeModel(["eins" ,"zwei"], "drei")
creates two columns with Header "Eins" und "Zwei" and you pass "drei" as data?!
On purpose?! -
@Pl45m4 said in modified Qt example editabletreemodel:
ProjectsTreeModel(["eins" ,"zwei"], "drei")
creates two columns with Header "Eins" und "Zwei" and you pass "drei" as data?!
On purpose?!Yes, I just have to give
data
and this"drei"
is a placeholder. It is not used in the model anyway. -
Haven't checked everything, but I think it has to do with the fact that you commented this line:
# self.setup_model_data(data.split("\n"), self.root_item)
Check the impl. of this function:
def setup_model_data(self, lines: list, parent: TreeItem): parents = [parent] indentations = [0] for line in lines: line = line.rstrip() if line and "\t" in line: position = 0 while position < len(line): if line[position] != " ": break position += 1 column_data = line[position:].split("\t") column_data = [string for string in column_data if string] if position > indentations[-1]: if parents[-1].child_count() > 0: parents.append(parents[-1].last_child()) indentations.append(position) else: while position < indentations[-1] and parents: parents.pop() indentations.pop() parent: TreeItem = parents[-1] col_count = self.root_item.column_count() parent.insert_children(parent.child_count(), 1, col_count) for column in range(len(column_data)): child = parent.last_child() child.set_data(column, column_data[column])
It sets up the model/view to prepare/display the data properly.
Just callinginsertItem
is not enough for a custom editable model implementation -