QTreeWidget

The QTreeWidget class provides a tree view that uses a predefined tree model. More

Inheritance diagram of PySide2.QtWidgets.QTreeWidget

Synopsis

Functions

Virtual functions

Slots

Signals

Detailed Description

../../_images/windows-treeview.png

The QTreeWidget class is a convenience class that provides a standard tree widget with a classic item-based interface similar to that used by the QListView class in Qt 3. This class is based on Qt’s Model/View architecture and uses a default model to hold items, each of which is a QTreeWidgetItem .

Developers who do not need the flexibility of the Model/View framework can use this class to create simple hierarchical lists very easily. A more flexible approach involves combining a QTreeView with a standard item model. This allows the storage of data to be separated from its representation.

In its simplest form, a tree widget can be constructed in the following way:

treeWidget = QTreeWidget()
treeWidget.setColumnCount(1)
items = []
for i in range(10):
    items.append(QTreeWidgetItem(None, QStringList(QString("item: %1").arg(i))))
treeWidget.insertTopLevelItems(None, items)

Before items can be added to the tree widget, the number of columns must be set with setColumnCount() . This allows each item to have one or more labels or other decorations. The number of columns in use can be found with the columnCount() function.

The tree can have a header that contains a section for each column in the widget. It is easiest to set up the labels for each section by supplying a list of strings with setHeaderLabels() , but a custom header can be constructed with a QTreeWidgetItem and inserted into the tree with the setHeaderItem() function.

The items in the tree can be sorted by column according to a predefined sort order. If sorting is enabled, the user can sort the items by clicking on a column header. Sorting can be enabled or disabled by calling setSortingEnabled() . The isSortingEnabled() function indicates whether sorting is enabled.

class PySide2.QtWidgets.QTreeWidget([parent=None])

Constructs a tree widget with the given parent .

PySide2.QtWidgets.QTreeWidget.addTopLevelItem(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

Appends the item as a top-level item in the widget.

PySide2.QtWidgets.QTreeWidget.addTopLevelItems(items)
Parameters:

items

Appends the list of items as a top-level items in the widget.

PySide2.QtWidgets.QTreeWidget.clear()

Clears the tree widget by removing all of its items and selections.

Note

Since each item is removed from the tree widget before being deleted, the return value of treeWidget() will be invalid when called from an item’s destructor.

PySide2.QtWidgets.QTreeWidget.closePersistentEditor(item[, column=0])
Parameters:

Closes the persistent editor for the item in the given column .

This function has no effect if no persistent editor is open for this combination of item and column.

PySide2.QtWidgets.QTreeWidget.collapseItem(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

Closes the item . This causes the tree containing the item’s children to be collapsed.

PySide2.QtWidgets.QTreeWidget.columnCount()
Return type:

int

This property holds the number of columns displayed in the tree widget.

By default, this property has a value of 1.

PySide2.QtWidgets.QTreeWidget.currentColumn()
Return type:

int

Returns the current column in the tree widget.

PySide2.QtWidgets.QTreeWidget.currentItem()
Return type:

PySide2.QtWidgets.QTreeWidgetItem

Returns the current item in the tree widget.

PySide2.QtWidgets.QTreeWidget.currentItemChanged(current, previous)
Parameters:
PySide2.QtWidgets.QTreeWidget.dropMimeData(parent, index, data, action)
Parameters:
Return type:

bool

Handles the data supplied by a drag and drop operation that ended with the given action in the index in the given parent item.

The default implementation returns true if the drop was successfully handled by decoding the mime data and inserting it into the model; otherwise it returns false .

PySide2.QtWidgets.QTreeWidget.editItem(item[, column=0])
Parameters:

Starts editing the item in the given column if it is editable.

PySide2.QtWidgets.QTreeWidget.expandItem(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

Expands the item . This causes the tree containing the item’s children to be expanded.

PySide2.QtWidgets.QTreeWidget.findItems(text, flags[, column=0])
Parameters:
  • text – str

  • flagsMatchFlags

  • column – int

Return type:

Returns a list of items that match the given text , using the given flags , in the given column .

PySide2.QtWidgets.QTreeWidget.headerItem()
Return type:

PySide2.QtWidgets.QTreeWidgetItem

Returns the item used for the tree widget’s header.

See also

setHeaderItem()

PySide2.QtWidgets.QTreeWidget.indexFromItem(item[, column=0])
Parameters:
Return type:

PySide2.QtCore.QModelIndex

Returns the QModelIndex associated with the given item in the given column .

Note

In Qt versions prior to 5.7, this function took a non-const item .

PySide2.QtWidgets.QTreeWidget.indexFromItem(item[, column=0])
Parameters:
Return type:

PySide2.QtCore.QModelIndex

Returns the QModelIndex associated with the given item in the given column .

Note

In Qt versions prior to 5.7, this function took a non-const item .

PySide2.QtWidgets.QTreeWidget.indexOfTopLevelItem(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

Return type:

int

Returns the index of the given top-level item , or -1 if the item cannot be found.

PySide2.QtWidgets.QTreeWidget.insertTopLevelItem(index, item)
Parameters:

Inserts the item at index in the top level in the view.

If the item has already been inserted somewhere else it won’t be inserted.

PySide2.QtWidgets.QTreeWidget.insertTopLevelItems(index, items)
Parameters:
  • index – int

  • items

Inserts the list of items at index in the top level in the view.

Items that have already been inserted somewhere else won’t be inserted.

PySide2.QtWidgets.QTreeWidget.invisibleRootItem()
Return type:

PySide2.QtWidgets.QTreeWidgetItem

Returns the tree widget’s invisible root item.

The invisible root item provides access to the tree widget’s top-level items through the QTreeWidgetItem API, making it possible to write functions that can treat top-level items and their children in a uniform way; for example, recursive functions.

PySide2.QtWidgets.QTreeWidget.isFirstItemColumnSpanned(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

Return type:

bool

Note

This function is deprecated.

Returns true if the given item is set to show only one section over all columns; otherwise returns false .

This function is deprecated. Use isFirstColumnSpanned() instead.

PySide2.QtWidgets.QTreeWidget.isItemExpanded(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

Return type:

bool

Note

This function is deprecated.

Returns true if the given item is open; otherwise returns false .

This function is deprecated. Use isExpanded() instead.

See also

itemExpanded()

PySide2.QtWidgets.QTreeWidget.isItemHidden(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

Return type:

bool

Note

This function is deprecated.

Returns true if the item is explicitly hidden, otherwise returns false .

This function is deprecated. Use isHidden() instead.

PySide2.QtWidgets.QTreeWidget.isItemSelected(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

Return type:

bool

Note

This function is deprecated.

Returns true if the item is selected; otherwise returns false .

This function is deprecated. Use isSelected() instead.

PySide2.QtWidgets.QTreeWidget.isPersistentEditorOpen(item[, column=0])
Parameters:
Return type:

bool

Returns whether a persistent editor is open for item item in column column .

PySide2.QtWidgets.QTreeWidget.itemAbove(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

Return type:

PySide2.QtWidgets.QTreeWidgetItem

Returns the item above the given item .

PySide2.QtWidgets.QTreeWidget.itemActivated(item, column)
Parameters:
PySide2.QtWidgets.QTreeWidget.itemAt(x, y)
Parameters:
  • x – int

  • y – int

Return type:

PySide2.QtWidgets.QTreeWidgetItem

This is an overloaded function.

Returns a pointer to the item at the coordinates (x , y ). The coordinates are relative to the tree widget’s viewport() .

PySide2.QtWidgets.QTreeWidget.itemAt(p)
Parameters:

pPySide2.QtCore.QPoint

Return type:

PySide2.QtWidgets.QTreeWidgetItem

Returns a pointer to the item at the coordinates p . The coordinates are relative to the tree widget’s viewport() .

See also

visualItemRect()

PySide2.QtWidgets.QTreeWidget.itemBelow(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

Return type:

PySide2.QtWidgets.QTreeWidgetItem

Returns the item visually below the given item .

PySide2.QtWidgets.QTreeWidget.itemChanged(item, column)
Parameters:
PySide2.QtWidgets.QTreeWidget.itemClicked(item, column)
Parameters:
PySide2.QtWidgets.QTreeWidget.itemCollapsed(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

PySide2.QtWidgets.QTreeWidget.itemDoubleClicked(item, column)
Parameters:
PySide2.QtWidgets.QTreeWidget.itemEntered(item, column)
Parameters:
PySide2.QtWidgets.QTreeWidget.itemExpanded(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

PySide2.QtWidgets.QTreeWidget.itemFromIndex(index)
Parameters:

indexPySide2.QtCore.QModelIndex

Return type:

PySide2.QtWidgets.QTreeWidgetItem

Returns a pointer to the QTreeWidgetItem associated with the given index .

See also

indexFromItem()

PySide2.QtWidgets.QTreeWidget.itemPressed(item, column)
Parameters:
PySide2.QtWidgets.QTreeWidget.itemSelectionChanged()
PySide2.QtWidgets.QTreeWidget.itemWidget(item, column)
Parameters:
Return type:

PySide2.QtWidgets.QWidget

Returns the widget displayed in the cell specified by item and the given column .

See also

setItemWidget()

PySide2.QtWidgets.QTreeWidget.items(data)
Parameters:

dataPySide2.QtCore.QMimeData

Return type:

Returns an empty list

See also

mimeData()

PySide2.QtWidgets.QTreeWidget.mimeData(items)
Parameters:

items

Return type:

PySide2.QtCore.QMimeData

Returns an object that contains a serialized description of the specified items . The format used to describe the items is obtained from the mimeTypes() function.

If the list of items is empty, None is returned rather than a serialized empty list.

PySide2.QtWidgets.QTreeWidget.mimeTypes()
Return type:

list of strings

Returns a list of MIME types that can be used to describe a list of treewidget items.

See also

mimeData()

PySide2.QtWidgets.QTreeWidget.openPersistentEditor(item[, column=0])
Parameters:

Opens a persistent editor for the item in the given column .

PySide2.QtWidgets.QTreeWidget.removeItemWidget(item, column)
Parameters:

Removes the widget set in the given item in the given column .

PySide2.QtWidgets.QTreeWidget.scrollToItem(item[, hint=EnsureVisible])
Parameters:

Ensures that the item is visible, scrolling the view if necessary using the specified hint .

PySide2.QtWidgets.QTreeWidget.selectedItems()
Return type:

Returns a list of all selected non-hidden items.

PySide2.QtWidgets.QTreeWidget.setColumnCount(columns)
Parameters:

columns – int

This property holds the number of columns displayed in the tree widget.

By default, this property has a value of 1.

PySide2.QtWidgets.QTreeWidget.setCurrentItem(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

Sets the current item in the tree widget.

Unless the selection mode is NoSelection , the item is also selected.

PySide2.QtWidgets.QTreeWidget.setCurrentItem(item, column)
Parameters:

Sets the current item in the tree widget and the current column to column .

See also

currentItem()

PySide2.QtWidgets.QTreeWidget.setCurrentItem(item, column, command)
Parameters:

Sets the current item in the tree widget and the current column to column , using the given command .

See also

currentItem()

PySide2.QtWidgets.QTreeWidget.setFirstItemColumnSpanned(item, span)
Parameters:

Note

This function is deprecated.

Sets the given item to only show one section for all columns if span is true; otherwise the item will show one section per column.

This function is deprecated. Use setFirstColumnSpanned() instead.

PySide2.QtWidgets.QTreeWidget.setHeaderItem(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

Sets the header item for the tree widget. The label for each column in the header is supplied by the corresponding label in the item.

The tree widget takes ownership of the item.

PySide2.QtWidgets.QTreeWidget.setHeaderLabel(label)
Parameters:

label – str

Same as setHeaderLabels ( QStringList (label )).

PySide2.QtWidgets.QTreeWidget.setHeaderLabels(labels)
Parameters:

labels – list of strings

Adds a column in the header for each item in the labels list, and sets the label for each column.

Note that won’t remove existing columns.

PySide2.QtWidgets.QTreeWidget.setItemExpanded(item, expand)
Parameters:

Note

This function is deprecated.

Sets the item referred to by item to either closed or opened, depending on the value of expand .

This function is deprecated. Use setExpanded() instead.

PySide2.QtWidgets.QTreeWidget.setItemHidden(item, hide)
Parameters:

Note

This function is deprecated.

Hides the given item if hide is true; otherwise shows the item.

This function is deprecated. Use setHidden() instead.

PySide2.QtWidgets.QTreeWidget.setItemSelected(item, select)
Parameters:

Note

This function is deprecated.

If select is true, the given item is selected; otherwise it is deselected.

This function is deprecated. Use setSelected() instead.

PySide2.QtWidgets.QTreeWidget.setItemWidget(item, column, widget)
Parameters:

Sets the given widget to be displayed in the cell specified by the given item and column .

The given widget ‘s autoFillBackground property must be set to true, otherwise the widget’s background will be transparent, showing both the model data and the tree widget item.

This function should only be used to display static content in the place of a tree widget item. If you want to display custom dynamic content or implement a custom editor widget, use QTreeView and subclass QStyledItemDelegate instead.

This function cannot be called before the item hierarchy has been set up, i.e., the QTreeWidgetItem that will hold widget must have been added to the view before widget is set.

Note

The tree takes ownership of the widget.

PySide2.QtWidgets.QTreeWidget.sortColumn()
Return type:

int

Returns the column used to sort the contents of the widget.

See also

sortItems()

PySide2.QtWidgets.QTreeWidget.sortItems(column, order)
Parameters:

Sorts the items in the widget in the specified order by the values in the given column .

See also

sortColumn()

PySide2.QtWidgets.QTreeWidget.supportedDropActions()
Return type:

DropActions

Returns the drop actions supported by this view.

See also

DropActions

PySide2.QtWidgets.QTreeWidget.takeTopLevelItem(index)
Parameters:

index – int

Return type:

PySide2.QtWidgets.QTreeWidgetItem

Removes the top-level item at the given index in the tree and returns it, otherwise returns None ;

PySide2.QtWidgets.QTreeWidget.topLevelItem(index)
Parameters:

index – int

Return type:

PySide2.QtWidgets.QTreeWidgetItem

Returns the top level item at the given index , or None if the item does not exist.

PySide2.QtWidgets.QTreeWidget.topLevelItemCount()
Return type:

int

This property holds the number of top-level items.

By default, this property has a value of 0.

PySide2.QtWidgets.QTreeWidget.visualItemRect(item)
Parameters:

itemPySide2.QtWidgets.QTreeWidgetItem

Return type:

PySide2.QtCore.QRect

Returns the rectangle on the viewport occupied by the item at item .

See also

itemAt()