QTreeWidget Class
The QTreeWidget class provides a tree view that uses a predefined tree model. More...
| Header: | #include <QTreeWidget> |
| qmake: | QT += widgets |
| Inherits: | QTreeView |
Properties
- columnCount : int
- topLevelItemCount : const int
Public Functions
| int | columnCount() const |
| void | setColumnCount(int columns) |
| int | topLevelItemCount() const |
Detailed Description

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(static_cast<QTreeWidget *>(nullptr), 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.
See also QTreeWidgetItem, QTreeWidgetItemIterator, QTreeView, Model/View Programming, and Settings Editor Example.
Property Documentation
columnCount : int
This property holds the number of columns displayed in the tree widget
By default, this property has a value of 1.
Access functions:
| int | columnCount() const |
| void | setColumnCount(int columns) |
topLevelItemCount : const int
This property holds the number of top-level items
By default, this property has a value of 0.
Access functions:
| int | topLevelItemCount() const |
See also columnCount() and currentItem().