Skip to content

February 11, 2016

PyQt: How to hide top level nodes in tree view

by Joe Kuan

In Qt treeview, hidding top level nodes is different to hidding lower level nodes. Lets start with a simple treeview example with two non-empty branches. Here is the code:

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

# ---------------------------------------------------------------------
class MainWindow(QMainWindow):
    def __init__(self, parent=None):

        self.setWindowTitle("Treeview Example")

        self.treeview = QTreeView(self)

        model = QStandardItemModel()
        rootNode = model.invisibleRootItem()
        branch1 = QStandardItem("Branch 1")
        branch1.appendRow([QStandardItem("Child A"), None])
        childnode = QStandardItem("Child B")
        branch1.appendRow([childnode, None])
        branch2 = QStandardItem("Branch 2")
        branch2.appendRow([QStandardItem("Child C"), None])
        branch2.appendRow([QStandardItem("Child D"), None])
        rootNode.appendRow([ branch1, None ])
        rootNode.appendRow([ branch2, None ])
        self.treeview.setColumnWidth(0, 150)


# ---------------------------------------------------------------------

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = MainWindow()

Above code creates a treeview with 2 branches both with 2 child nodes. Below is the screen shot with all branches expanded:
Let say we want to hide the child node B, we can simple call setRowHidden method, like below:

self.treeview.setRowHidden(1, branch1.index(), True)


self.treeview.setRowHidden(1, childnode.parent().index(), True)

This hides the 2nd row from the branch1 node. Here is the output:

Let say we want to hide the top level node (Branch 1) and we issue the following call:

self.treeview.setRowHidden(0, branch1.parent().index(), True)

However, this gives a runtime error:
This is because the parent behaviour between top level and sublevel nodes is different. For top level nodes, the call for retrieving parent will return None (even though it is the child of the invisible root node) whereas for sublevel nodes the call will return the parent node. Also this behaviour is used to distinguish whether a node is at the top level.
If we want to hide the top level node, we have to supply the parent index in a different way.

self.treeview.setRowHidden(0, model.invisibleRootItem().index(), True)


Read more from Programming, Python, Qt

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: