|  | Home · All Classes · Modules

# QTreeWidget Class Reference
[QtGui module]

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

Inherits QTreeView.

### Methods

- __init__ (self, QWidget parent = None)
- addTopLevelItem (self, QTreeWidgetItem)
- addTopLevelItems (self, QList<QTreeWidgetItem *>list-of-QTreeWidgetItem)
- clear (self)
- closePersistentEditor (self, QTreeWidgetItem, int column = 0)
- collapseItem (self, QTreeWidgetItem)
- int columnCount (self)
- int currentColumn (self)
- QTreeWidgetItem currentItem (self)
- dropEvent (self, QDropEvent)
- bool dropMimeData (self, QTreeWidgetItem, int, QMimeData, Qt.DropAction)
- editItem (self, QTreeWidgetItem, int column = 0)
- bool event (self, QEvent)
- expandItem (self, QTreeWidgetItem)
- QList<QTreeWidgetItem *>list-of-QTreeWidgetItem findItems (self, QStringQString, Qt.MatchFlags, int column = 0)
- QTreeWidgetItem headerItem (self)
- QModelIndex indexFromItem (self, QTreeWidgetItem, int column = 0)
- int indexOfTopLevelItem (self, QTreeWidgetItem)
- insertTopLevelItem (self, int, QTreeWidgetItem)
- insertTopLevelItems (self, int, QList<QTreeWidgetItem *>list-of-QTreeWidgetItem)
- QTreeWidgetItem invisibleRootItem (self)
- bool isFirstItemColumnSpanned (self, QTreeWidgetItem)
- bool isItemExpanded (self, QTreeWidgetItem)
- bool isItemHidden (self, QTreeWidgetItem)
- bool isItemSelected (self, QTreeWidgetItem)
- bool isSortingEnabled (self)
- QTreeWidgetItem itemAbove (self, QTreeWidgetItem)
- QTreeWidgetItem itemAt (self, QPoint)
- QTreeWidgetItem itemAt (self, int, int)
- QTreeWidgetItem itemBelow (self, QTreeWidgetItem)
- QTreeWidgetItem itemFromIndex (self, QModelIndex)
- QList<QTreeWidgetItem *>list-of-QTreeWidgetItem items (self, QMimeData)
- QWidget itemWidget (self, QTreeWidgetItem, int)
- QMimeData mimeData (self, QList<QTreeWidgetItem *>list-of-QTreeWidgetItem)
- QStringListQStringList mimeTypes (self)
- openPersistentEditor (self, QTreeWidgetItem, int column = 0)
- removeItemWidget (self, QTreeWidgetItem, int)
- scrollToItem (self, QTreeWidgetItem, QAbstractItemView.ScrollHint hint = QAbstractItemView.EnsureVisible)
- QList<QTreeWidgetItem *>list-of-QTreeWidgetItem selectedItems (self)
- setColumnCount (self, int)
- setCurrentItem (self, QTreeWidgetItem)
- setCurrentItem (self, QTreeWidgetItem, int)
- setCurrentItem (self, QTreeWidgetItem, int, QItemSelectionModel.SelectionFlags)
- setFirstItemColumnSpanned (self, QTreeWidgetItem, bool)
- setHeaderItem (self, QTreeWidgetItem)
- setHeaderLabel (self, QStringQString)
- setHeaderLabels (self, QStringListQStringList)
- setItemExpanded (self, QTreeWidgetItem, bool)
- setItemHidden (self, QTreeWidgetItem, bool)
- setItemSelected (self, QTreeWidgetItem, bool)
- setItemWidget (self, QTreeWidgetItem, int, QWidget)
- setSelectionModel (self, QItemSelectionModel)
- setSortingEnabled (self, bool)
- int sortColumn (self)
- sortItems (self, int, Qt.SortOrder)
- Qt.DropActions supportedDropActions (self)
- QTreeWidgetItem takeTopLevelItem (self, int)
- QTreeWidgetItem topLevelItem (self, int)
- int topLevelItemCount (self)
- QRect visualItemRect (self, QTreeWidgetItem)

### Qt Signals

- void currentItemChanged (QTreeWidgetItem *,QTreeWidgetItem *)
- void itemActivated (QTreeWidgetItem *,int)
- void itemChanged (QTreeWidgetItem *,int)
- void itemClicked (QTreeWidgetItem *,int)
- void itemCollapsed (QTreeWidgetItem *)
- void itemDoubleClicked (QTreeWidgetItem *,int)
- void itemEntered (QTreeWidgetItem *,int)
- void itemExpanded (QTreeWidgetItem *)
- void itemPressed (QTreeWidgetItem *,int)
- void itemSelectionChanged ()

## Detailed Description

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

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:

     QTreeWidget *treeWidget = new QTreeWidget();
     treeWidget->setColumnCount(1);
     QList<QTreeWidgetItem *> items;
     for (int i = 0; i < 10; ++i)
         items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i))));
     treeWidget->insertTopLevelItems(0, 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.

A Windows XP style tree widget. | A Macintosh style tree widget. | A Plastique style tree widget.

See also QTreeWidgetItem, QTreeWidgetItemIterator, QTreeView, Model/View Programming, and Settings Editor Example.

## Method Documentation

### QTreeWidget.__init__ (self, QWidget parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a tree widget with the given parent.

### QTreeWidget.addTopLevelItem (self, QTreeWidgetItem)

The QTreeWidgetItem argument has it's ownership transferred to Qt.

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

This function was introduced in Qt 4.1.

See also insertTopLevelItem().

### QTreeWidget.addTopLevelItems (self, QList<QTreeWidgetItem *>list-of-QTreeWidgetItem)

The QList<QTreeWidgetItem *>list-of-QTreeWidgetItem argument has it's ownership transferred to Qt.

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

See also insertTopLevelItems().

### QTreeWidget.clear (self)

This method is also a Qt slot with the C++ signature `void 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 QTreeWidgetItem.treeWidget() will be invalid when called from an item's destructor.

See also takeTopLevelItem(), topLevelItemCount(), and columnCount().

### QTreeWidget.closePersistentEditor (self, QTreeWidgetItem, int column = 0)

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.

See also openPersistentEditor().

### QTreeWidget.collapseItem (self, QTreeWidgetItem)

This method is also a Qt slot with the C++ signature `void collapseItem(const QTreeWidgetItem *)`.

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

See also expandItem(), currentItem(), itemAt(), and topLevelItem().

### int QTreeWidget.columnCount (self)

### int QTreeWidget.currentColumn (self)

Returns the current column in the tree widget.

This function was introduced in Qt 4.1.

See also setCurrentItem() and columnCount().

### QTreeWidgetItem QTreeWidget.currentItem (self)

Returns the current item in the tree widget.

See also setCurrentItem() and currentItemChanged().

### QTreeWidget.dropEvent (self, QDropEvent)

Reimplemented from QWidget.dropEvent().

### bool QTreeWidget.dropMimeData (self, QTreeWidgetItem, int, QMimeData, Qt.DropAction)

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.

See also supportedDropActions().

### QTreeWidget.editItem (self, QTreeWidgetItem, int column = 0)

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

### bool QTreeWidget.event (self, QEvent)

Reimplemented from QObject.event().

### QTreeWidget.expandItem (self, QTreeWidgetItem)

This method is also a Qt slot with the C++ signature `void expandItem(const QTreeWidgetItem *)`.

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

See also collapseItem(), currentItem(), itemAt(), topLevelItem(), and itemExpanded().

### QList<QTreeWidgetItem *>list-of-QTreeWidgetItem QTreeWidget.findItems (self, QStringQString, Qt.MatchFlags, int column = 0)

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

### QTreeWidgetItem QTreeWidget.headerItem (self)

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

See also setHeaderItem().

### QModelIndex QTreeWidget.indexFromItem (self, QTreeWidgetItem, int column = 0)

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

See also itemFromIndex() and topLevelItem().

### int QTreeWidget.indexOfTopLevelItem (self, QTreeWidgetItem)

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

See also sortItems() and topLevelItemCount().

### QTreeWidget.insertTopLevelItem (self, int, QTreeWidgetItem)

The QTreeWidgetItem argument has it's ownership transferred to Qt.

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

If the item has already been inserted somewhere else it wont be inserted.

See also addTopLevelItem() and columnCount().

### QTreeWidget.insertTopLevelItems (self, int, QList<QTreeWidgetItem *>list-of-QTreeWidgetItem)

The QList<QTreeWidgetItem *>list-of-QTreeWidgetItem argument has it's ownership transferred to Qt.

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

Items that have already been inserted somewhere else wont be inserted.

This function was introduced in Qt 4.1.

See also addTopLevelItems().

### QTreeWidgetItem QTreeWidget.invisibleRootItem (self)

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.

This function was introduced in Qt 4.2.

### bool QTreeWidget.isFirstItemColumnSpanned (self, QTreeWidgetItem)

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

This function was introduced in Qt 4.3.

See also setFirstItemColumnSpanned().

### bool QTreeWidget.isItemExpanded (self, QTreeWidgetItem)

### bool QTreeWidget.isItemHidden (self, QTreeWidgetItem)

### bool QTreeWidget.isItemSelected (self, QTreeWidgetItem)

### bool QTreeWidget.isSortingEnabled (self)

### QTreeWidgetItem QTreeWidget.itemAbove (self, QTreeWidgetItem)

Returns the item above the given item.

This function was introduced in Qt 4.3.

### QTreeWidgetItem QTreeWidget.itemAt (self, QPoint)

Returns a pointer to the item at the coordinates p.

See also visualItemRect().

### QTreeWidgetItem QTreeWidget.itemAt (self, int, int)

This is an overloaded function.

Returns a pointer to the item at the coordinates (x, y).

### QTreeWidgetItem QTreeWidget.itemBelow (self, QTreeWidgetItem)

Returns the item visually below the given item.

This function was introduced in Qt 4.3.

### QTreeWidgetItem QTreeWidget.itemFromIndex (self, QModelIndex)

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

See also indexFromItem().

### QList<QTreeWidgetItem *>list-of-QTreeWidgetItem QTreeWidget.items (self, QMimeData)

### QWidget QTreeWidget.itemWidget (self, QTreeWidgetItem, int)

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

Note: The tree takes ownership of the widget.

This function was introduced in Qt 4.1.

See also setItemWidget().

### QMimeData QTreeWidget.mimeData (self, QList<QTreeWidgetItem *>list-of-QTreeWidgetItem)

The QMimeData result

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, 0 is returned rather than a serialized empty list.

### QStringListQStringList QTreeWidget.mimeTypes (self)

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

See also mimeData().

### QTreeWidget.openPersistentEditor (self, QTreeWidgetItem, int column = 0)

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

See also closePersistentEditor().

### QTreeWidget.removeItemWidget (self, QTreeWidgetItem, int)

The QTreeWidgetItem argument

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

This function was introduced in Qt 4.3.

### QTreeWidget.scrollToItem (self, QTreeWidgetItem, QAbstractItemView.ScrollHint hint = QAbstractItemView.EnsureVisible)

This method is also a Qt slot with the C++ signature `void scrollToItem(const QTreeWidgetItem *,QAbstractItemView::ScrollHint = QAbstractItemView.EnsureVisible)`.

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

See also currentItem(), itemAt(), and topLevelItem().

### QList<QTreeWidgetItem *>list-of-QTreeWidgetItem QTreeWidget.selectedItems (self)

Returns a list of all selected non-hidden items.

See also itemSelectionChanged().

### QTreeWidget.setColumnCount (self, int)

### QTreeWidget.setCurrentItem (self, QTreeWidgetItem)

Sets the current item in the tree widget.

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

See also currentItem() and currentItemChanged().

### QTreeWidget.setCurrentItem (self, QTreeWidgetItem, int)

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

This function was introduced in Qt 4.1.

See also currentItem().

### QTreeWidget.setCurrentItem (self, QTreeWidgetItem, int, QItemSelectionModel.SelectionFlags)

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

This function was introduced in Qt 4.4.

See also currentItem().

### QTreeWidget.setFirstItemColumnSpanned (self, QTreeWidgetItem, bool)

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 was introduced in Qt 4.3.

See also isFirstItemColumnSpanned().

### QTreeWidget.setHeaderItem (self, 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.

See also headerItem() and setHeaderLabels().

### QTreeWidget.setHeaderLabel (self, QStringQString)

Same as setHeaderLabels(QStringList(label)).

This function was introduced in Qt 4.2.

### QTreeWidget.setHeaderLabels (self, QStringListQStringList)

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

Note that setHeaderLabels() won't remove existing columns.

See also setHeaderItem() and setHeaderLabel().

### QTreeWidget.setItemExpanded (self, QTreeWidgetItem, bool)

### QTreeWidget.setItemHidden (self, QTreeWidgetItem, bool)

### QTreeWidget.setItemSelected (self, QTreeWidgetItem, bool)

### QTreeWidget.setItemWidget (self, QTreeWidgetItem, int, QWidget)

The QWidget argument has it's ownership transferred to Qt.

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

This function was introduced in Qt 4.1.

See also itemWidget() and Delegate Classes.

### QTreeWidget.setSelectionModel (self, QItemSelectionModel)

Reimplemented from QAbstractItemView.setSelectionModel().

### QTreeWidget.setSortingEnabled (self, bool)

### int QTreeWidget.sortColumn (self)

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

This function was introduced in Qt 4.1.

See also sortItems().

### QTreeWidget.sortItems (self, int, Qt.SortOrder)

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

See also sortColumn().

### Qt.DropActions QTreeWidget.supportedDropActions (self)

Returns the drop actions supported by this view.

See also Qt.DropActions.

### QTreeWidgetItem QTreeWidget.takeTopLevelItem (self, int)

The QTreeWidgetItem result

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

See also insertTopLevelItem(), topLevelItem(), and topLevelItemCount().

### QTreeWidgetItem QTreeWidget.topLevelItem (self, int)

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

See also topLevelItemCount() and insertTopLevelItem().

### int QTreeWidget.topLevelItemCount (self)

### QRect QTreeWidget.visualItemRect (self, QTreeWidgetItem)

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

See also itemAt().

## Qt Signal Documentation

### void currentItemChanged (QTreeWidgetItem *,QTreeWidgetItem *)

This is the default overload of this signal.

This signal is emitted when the current item changes. The current item is specified by current, and this replaces the previous current item.

See also setCurrentItem().

### void itemActivated (QTreeWidgetItem *,int)

This is the default overload of this signal.

This signal is emitted when the user activates an item by single- or double-clicking (depending on the platform, i.e. on the QStyle.SH_ItemView_ActivateItemOnSingleClick style hint) or pressing a special key (e.g., Enter).

The specified item is the item that was clicked, or 0 if no item was clicked. The column is the item's column that was clicked, or -1 if no item was clicked.

### void itemChanged (QTreeWidgetItem *,int)

This is the default overload of this signal.

This signal is emitted when the contents of the column in the specified item changes.

### void itemClicked (QTreeWidgetItem *,int)

This is the default overload of this signal.

This signal is emitted when the user clicks inside the widget.

The specified item is the item that was clicked. The column is the item's column that was clicked. If no item was clicked, no signal will be emitted.

### void itemCollapsed (QTreeWidgetItem *)

This is the default overload of this signal.

This signal is emitted when the specified item is collapsed so that none of its children are displayed.

Note: This signal will not be emitted if an item changes its state when collapseAll() is invoked.

See also QTreeWidgetItem.isExpanded(), itemExpanded(), and collapseItem().

### void itemDoubleClicked (QTreeWidgetItem *,int)

This is the default overload of this signal.

This signal is emitted when the user double clicks inside the widget.

The specified item is the item that was clicked, or 0 if no item was clicked. The column is the item's column that was clicked. If no item was double clicked, no signal will be emitted.

### void itemEntered (QTreeWidgetItem *,int)

This is the default overload of this signal.

This signal is emitted when the mouse cursor enters an item over the specified column. QTreeWidget mouse tracking needs to be enabled for this feature to work.

### void itemExpanded (QTreeWidgetItem *)

This is the default overload of this signal.

This signal is emitted when the specified item is expanded so that all of its children are displayed.

Note: This signal will not be emitted if an item changes its state when expandAll() is invoked.

See also setItemExpanded(), QTreeWidgetItem.isExpanded(), itemCollapsed(), and expandItem().

### void itemPressed (QTreeWidgetItem *,int)

This is the default overload of this signal.

This signal is emitted when the user presses a mouse button inside the widget.

The specified item is the item that was clicked, or 0 if no item was clicked. The column is the item's column that was clicked, or -1 if no item was clicked.

### void itemSelectionChanged ()

This is the default overload of this signal.

This signal is emitted when the selection changes in the tree widget. The current selection can be found with selectedItems().