<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">I was trying to understand if there were alternate possibilities of moving a tree structure over the first column (besides QHeaderView.swapSections), and I found something strange (to my understanding) about the behavior of QTreeView when trying to insert a column at index 0.<div><br>I know that doing it is not suggested, but still.<br>I posted a question on SO some hours ago, so forgive me if I'm just pasting it here:</div><div><br></div><div><div>If I add a child to an item that is not in the first column, it is obviously not shown, since only children of the root index in first column are expanded; but it seems that when the new column is inserted, the existing children of the previous first column are "inherited" by the new item.</div><div><br></div><div><div><font face="monospace">class TreeTest(QtWidgets.QWidget):</font></div><div><font face="monospace">    def __init__(self):</font></div><div><font face="monospace">        super().__init__()</font></div><div><font face="monospace">        layout = QtWidgets.QVBoxLayout(self)</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">        insertColBtn = QtWidgets.QPushButton('insert column')</font></div><div><font face="monospace">        layout.addWidget(insertColBtn)</font></div><div><font face="monospace">        insertColBtn.clicked.connect(self.insertColumn)</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">        insertColAfterBtn = QtWidgets.QPushButton('insert child and insert column')</font></div><div><font face="monospace">        layout.addWidget(insertColAfterBtn)</font></div><div><font face="monospace">        insertColAfterBtn.clicked.connect(self.insertColumnAfter)</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">        removeColBtn = QtWidgets.QPushButton('remove column')</font></div><div><font face="monospace">        layout.addWidget(removeColBtn)</font></div><div><font face="monospace">        removeColBtn.clicked.connect(self.removeColumn)</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">        self.tree = QtWidgets.QTreeView()</font></div><div><font face="monospace">        layout.addWidget(self.tree)</font></div><div><font face="monospace">        self.model = QtGui.QStandardItemModel()</font></div><div><font face="monospace">        self.tree.setModel(self.model)</font></div><div><font face="monospace">        self.parent1 = QtGui.QStandardItem('parent 1')</font></div><div><font face="monospace">        self.parent2 = QtGui.QStandardItem('parent 2')</font></div><div><font face="monospace">        self.model.appendRow([self.parent1, self.parent2])</font></div><div><font face="monospace">        self.child = QtGui.QStandardItem('child of parent 1')</font></div><div><font face="monospace">        self.parent1.appendRow(self.child)</font></div><div><font face="monospace">        self.parent2.appendRow(QtGui.QStandardItem('child of parent 2'))</font></div><div><font face="monospace">        self.tree.expandAll()</font></div><div><font face="monospace">        self.tree.header().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">    def insertColumn(self):</font></div><div><font face="monospace">        newParent = QtGui.QStandardItem('new parent')</font></div><div><font face="monospace">        self.model.insertColumn(0, [newParent])</font></div><div><font face="monospace">        newChild = QtGui.QStandardItem('new column child')</font></div><div><font face="monospace">        self.parent1.appendRow(newChild)</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">    def insertColumnAfter(self):</font></div><div><font face="monospace">        newParent = QtGui.QStandardItem('new parent')</font></div><div><font face="monospace">        newChild = QtGui.QStandardItem('new column child')</font></div><div><font face="monospace">        self.parent1.appendRow(newChild)</font></div><div><font face="monospace">        self.model.insertColumn(0, [newParent])</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">    def removeColumn(self):</font></div><div><font face="monospace">        self.model.removeColumn(0)</font></div></div><div><br></div><div>The program creates two items in the first two columns, each one with its own child item.</div><div>When the program is started (see treetest1.png), the item in the first column shows its child as expected, and the child of the second is hidden (obviously, according to the normal behavior of a QTreeView).</div><div><br></div><div>Then I insert a new first column, and *then* add a child to the previous parent (which is now on the second column); as you can see (treetest2.png), the previous child is now apparently become a child of the new item, while the new child item is not visible.</div><div>Also, the new parent item is not collapsible. *BUT* if I append an item to the new parent (I didn't add this to the code above), the "wrong" child item now correctly disappears.</div><div><br></div><div>Finally (treetest3.png), I try to remove the first column, and the previously added child is now visible.<br></div><div><br></div><div>Strangely enough, if a new child item of the previous first column is added before inserting the new column (insertColumnAfter()), it works "as expected", probably due to the delay item views usually have before laying out the items after a model change (treetest4.png).<br></div><div><br></div><div><div>Again, I realize this is not the "standard" way to deal with a tree model.</div><div>Still, I'd like to understand what's going on here, whether it's an expected behavior (probably by design, even if not perfect) or some kind of bug.</div><div><br></div><div>I've been able to reproduce this behavior with PyQt up to 5.12.4, but an user on SO reported that it also happened on Qt 5.14.1, and we both suspect that it might not be PyQt related at all.</div></div><div><br></div><div>Thank you,</div><div>Maurizio</div><div><br></div><div><br></div>-- <br><div dir="ltr" class="gmail_signature">È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi<br><a href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div></div></div></div></div></div></div></div></div>