|  | Home · Overviews · Examples

# QTreeWidget Class Reference
[com.trolltech.qt.gui module]

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

Inherits `QTreeView`.

## 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`, `QTreeView`, Model/View Programming, and Settings Editor Example.