- Qt 6.9
- Qt Widgets
- C++ Classes
- QTreeWidgetItem
- Qt 6.9.2 Reference Documentation

# QTreeWidgetItem Class

The QTreeWidgetItem class provides an item for use with the QTreeWidget convenience class. More...

Header: | `#include <QTreeWidgetItem>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Widgets)` `target_link_libraries(mytarget PRIVATE Qt6::Widgets)`
qmake: | `QT += widgets`

- List of all members, including inherited members

## Public Types

enum | ChildIndicatorPolicy { ShowIndicator, DontShowIndicator, DontShowIndicatorWhenChildless }
enum | ItemType { Type, UserType }

## Public Functions

| QTreeWidgetItem(int type = Type)
| QTreeWidgetItem(QTreeWidgetItem *parent, int type = Type)
| QTreeWidgetItem(const QStringList &strings, int type = Type)
| QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &strings, int type = Type)
| QTreeWidgetItem(const QTreeWidgetItem &other)
virtual | ~QTreeWidgetItem()
void | addChild(QTreeWidgetItem *child)
void | addChildren(const QList<QTreeWidgetItem *> &children)
QTreeWidgetItem::ChildIndicatorPolicy | childIndicatorPolicy() const
virtual QTreeWidgetItem * | clone() const
virtual QVariant | data(int column, int role) const
void | insertChild(int index, QTreeWidgetItem *child)
void | insertChildren(int index, const QList<QTreeWidgetItem *> &children)
virtual void | read(QDataStream &in)
void | removeChild(QTreeWidgetItem *child)
void | setChildIndicatorPolicy(QTreeWidgetItem::ChildIndicatorPolicy policy)
virtual void | setData(int column, int role, const QVariant &value)
QTreeWidgetItem * | takeChild(int index)
QList<QTreeWidgetItem *> | takeChildren()
virtual void | write(QDataStream &out) const
virtual bool | operator<(const QTreeWidgetItem &other) const
QTreeWidgetItem & | operator=(const QTreeWidgetItem &other)

## Protected Functions

void | emitDataChanged()

## Related Non-Members

QDataStream & | operator<<(QDataStream &out, const QTreeWidgetItem &item)
QDataStream & | operator>>(QDataStream &in, QTreeWidgetItem &item)

## Detailed Description

Tree widget items are used to hold rows of information for tree widgets. Rows usually contain several columns of data, each of which can contain a text label and an icon.

The QTreeWidgetItem class is a convenience class that replaces the QListViewItem class in Qt 3. It provides an item for use with the QTreeWidget class.

Items are usually constructed with a parent that is either a QTreeWidget (for top-level items) or a QTreeWidgetItem (for items on lower levels of the tree). For example, the following code constructs a top-level item to represent cities of the world, and adds a entry for Oslo as a child item:

         QTreeWidgetItem *cities = new QTreeWidgetItem(treeWidget);
         cities->setText(0, tr("Cities"));
         QTreeWidgetItem *osloItem = new QTreeWidgetItem(cities);
         osloItem->setText(0, tr("Oslo"));
         osloItem->setText(1, tr("Yes"));

Items can be added in a particular order by specifying the item they follow when they are constructed:

         QTreeWidgetItem *planets = new QTreeWidgetItem(treeWidget, cities);
         planets->setText(0, tr("Planets"));

Each column in an item can have its own background brush which is set with the setBackground() function. The current background brush can be found with background(). The text label for each column can be rendered with its own font and brush. These are specified with the setFont() and setForeground() functions, and read with font() and foreground().

The main difference between top-level items and those in lower levels of the tree is that a top-level item has no parent(). This information can be used to tell the difference between items, and is useful to know when inserting and removing items from the tree. Children of an item can be removed with takeChild() and inserted at a given index in the list of children with the insertChild() function.

By default, items are enabled, selectable, checkable, and can be the source of a drag and drop operation. Each item's flags can be changed by calling setFlags() with the appropriate value (see Qt::ItemFlags). Checkable items can be checked and unchecked with the setCheckState() function. The corresponding checkState() function indicates whether the item is currently checked.

### Subclassing

When subclassing QTreeWidgetItem to provide custom items, it is possible to define new types for them so that they can be distinguished from standard items. The constructors for subclasses that require this feature need to call the base class constructor with a new type value equal to or greater than UserType.

See also QTreeWidget, QTreeWidgetItemIterator, Model/View Programming, QListWidgetItem, and QTableWidgetItem.

## Member Type Documentation

### enum QTreeWidgetItem::ChildIndicatorPolicy

Constant | Value | Description
--- | --- | ---
`QTreeWidgetItem::ShowIndicator` | `0` | The controls for expanding and collapsing will be shown for this item even if there are no children.
`QTreeWidgetItem::DontShowIndicator` | `1` | The controls for expanding and collapsing will never be shown even if there are children. If the node is forced open the user will not be able to expand or collapse the item.
`QTreeWidgetItem::DontShowIndicatorWhenChildless` | `2` | The controls for expanding and collapsing will be shown if the item contains children.

### enum QTreeWidgetItem::ItemType

This enum describes the types that are used to describe tree widget items.

Constant | Value | Description
--- | --- | ---
`QTreeWidgetItem::Type` | `0` | The default type for tree widget items.
`QTreeWidgetItem::UserType` | `1000` | The minimum value for custom types. Values below UserType are reserved by Qt.

You can define new user types in QTreeWidgetItem subclasses to ensure that custom items are treated specially; for example, when items are sorted.

See also type().

## Member Function Documentation

### `[explicit]` QTreeWidgetItem::QTreeWidgetItem(int type = Type)

Constructs a tree widget item of the specified type. The item must be inserted into a tree widget.

See also type().

### `[explicit]` QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, int type = Type)

Constructs a tree widget item and append it to the given parent.

See also type().

### `[explicit]` QTreeWidgetItem::QTreeWidgetItem(const QStringList &strings, int type = Type)

Constructs a tree widget item of the specified type. The item must be inserted into a tree widget. The given list of strings will be set as the item text for each column in the item.

See also type().

### QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &strings, int type = Type)

Constructs a tree widget item and append it to the given parent. The given list of strings will be set as the item text for each column in the item.

See also type().

### QTreeWidgetItem::QTreeWidgetItem(const QTreeWidgetItem &other)

Constructs a copy of other. Note that type() and treeWidget() are not copied.

This function is useful when reimplementing clone().

See also data() and flags().

### `[virtual noexcept]` QTreeWidgetItem::~QTreeWidgetItem()

Destroys this tree widget item.

The item will be removed from QTreeWidgets to which it has been added. This makes it safe to delete an item at any time.

### void QTreeWidgetItem::addChild(QTreeWidgetItem *child)

Appends the child item to the list of children.

See also insertChild() and takeChild().

### void QTreeWidgetItem::addChildren(const QList<QTreeWidgetItem *> &children)

Appends the given list of children to the item.

See also insertChildren() and takeChildren().

### QTreeWidgetItem::ChildIndicatorPolicy QTreeWidgetItem::childIndicatorPolicy() const

Returns the item indicator policy. This policy decides when the tree branch expand/collapse indicator is shown.

See also setChildIndicatorPolicy().

### `[virtual]` QTreeWidgetItem *QTreeWidgetItem::clone() const

Creates a deep copy of the item and of its children.

### `[virtual]` QVariant QTreeWidgetItem::data(int column, int role) const

Returns the value for the item's column and role.

See also setData().

### `[protected]` void QTreeWidgetItem::emitDataChanged()

Causes the model associated with this item to emit a dataChanged() signal for this item.

You normally only need to call this function if you have subclassed QTreeWidgetItem and reimplemented data() and/or setData().

See also setData().

### void QTreeWidgetItem::insertChild(int index, QTreeWidgetItem *child)

Inserts the child item at index in the list of children.

If the child has already been inserted somewhere else it won't be inserted again.

### void QTreeWidgetItem::insertChildren(int index, const QList<QTreeWidgetItem *> &children)

Inserts the given list of children into the list of the item children at index .

Children that have already been inserted somewhere else won't be inserted.

### `[virtual]` void QTreeWidgetItem::read(QDataStream &in)

Reads the item from stream in. This only reads data into a single item.

See also write().

### void QTreeWidgetItem::removeChild(QTreeWidgetItem *child)

Removes the given item indicated by child. The removed item will not be deleted.

### void QTreeWidgetItem::setChildIndicatorPolicy(QTreeWidgetItem::ChildIndicatorPolicy policy)

Sets the item indicator policy. This policy decides when the tree branch expand/collapse indicator is shown. The default value is DontShowIndicatorWhenChildless.

See also childIndicatorPolicy().

### `[virtual]` void QTreeWidgetItem::setData(int column, int role, const QVariant &value)

Sets the value for the item's column and role to the given value.

The role describes the type of data specified by value, and is defined by the Qt::ItemDataRole enum.

Note: The default implementation treats Qt::EditRole and Qt::DisplayRole as referring to the same data.

See also data().

### QTreeWidgetItem *QTreeWidgetItem::takeChild(int index)

Removes the item at index and returns it, otherwise return 0.

### QList<QTreeWidgetItem *> QTreeWidgetItem::takeChildren()

Removes the list of children and returns it, otherwise returns an empty list.

### `[virtual]` void QTreeWidgetItem::write(QDataStream &out) const

Writes the item to stream out. This only writes data from one single item.

See also read().

### `[virtual]` bool QTreeWidgetItem::operator<(const QTreeWidgetItem &other) const

Returns `true` if the text in the item is less than the text in the other item, otherwise returns `false`.

### QTreeWidgetItem &QTreeWidgetItem::operator=(const QTreeWidgetItem &other)

Assigns other's data and flags to this item. Note that type() and treeWidget() are not copied.

This function is useful when reimplementing clone().

See also data() and flags().

## Related Non-Members

### QDataStream &operator<<(QDataStream &out, const QTreeWidgetItem &item)

Writes the tree widget item item to stream out.

This operator uses QTreeWidgetItem::write().

See also Serializing Qt Data Types.

### QDataStream &operator>>(QDataStream &in, QTreeWidgetItem &item)

Reads a tree widget item from stream in into item.

This operator uses QTreeWidgetItem::read().

See also Serializing Qt Data Types.