[PyQt] Strange behavior of QTreeView when inserting columns at index 0

Maurizio Berti maurizio.berti at gmail.com
Fri Mar 27 15:12:13 GMT 2020


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.

I know that doing it is not suggested, but still.
I posted a question on SO some hours ago, so forgive me if I'm just pasting
it here:

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.

class TreeTest(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        layout = QtWidgets.QVBoxLayout(self)

        insertColBtn = QtWidgets.QPushButton('insert column')
        layout.addWidget(insertColBtn)
        insertColBtn.clicked.connect(self.insertColumn)

        insertColAfterBtn = QtWidgets.QPushButton('insert child and insert
column')
        layout.addWidget(insertColAfterBtn)
        insertColAfterBtn.clicked.connect(self.insertColumnAfter)

        removeColBtn = QtWidgets.QPushButton('remove column')
        layout.addWidget(removeColBtn)
        removeColBtn.clicked.connect(self.removeColumn)

        self.tree = QtWidgets.QTreeView()
        layout.addWidget(self.tree)
        self.model = QtGui.QStandardItemModel()
        self.tree.setModel(self.model)
        self.parent1 = QtGui.QStandardItem('parent 1')
        self.parent2 = QtGui.QStandardItem('parent 2')
        self.model.appendRow([self.parent1, self.parent2])
        self.child = QtGui.QStandardItem('child of parent 1')
        self.parent1.appendRow(self.child)
        self.parent2.appendRow(QtGui.QStandardItem('child of parent 2'))
        self.tree.expandAll()

self.tree.header().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)

    def insertColumn(self):
        newParent = QtGui.QStandardItem('new parent')
        self.model.insertColumn(0, [newParent])
        newChild = QtGui.QStandardItem('new column child')
        self.parent1.appendRow(newChild)

    def insertColumnAfter(self):
        newParent = QtGui.QStandardItem('new parent')
        newChild = QtGui.QStandardItem('new column child')
        self.parent1.appendRow(newChild)
        self.model.insertColumn(0, [newParent])

    def removeColumn(self):
        self.model.removeColumn(0)

The program creates two items in the first two columns, each one with its
own child item.
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).

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.
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.

Finally (treetest3.png), I try to remove the first column, and the
previously added child is now visible.

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).

Again, I realize this is not the "standard" way to deal with a tree model.
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.

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.

Thank you,
Maurizio


-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20200327/1b92802e/attachment-0001.htm>


More information about the PyQt mailing list