6.0.0-rc4Source

QTreeWidgetItem Class

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

Header#include <QTreeWidgetItem>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QTreeWidgetItem(int type = Type)
QTreeWidgetItem(const QStringList &strings, int type = Type)
QTreeWidgetItem(QTreeWidget *treeview, int type = Type)
QTreeWidgetItem(QTreeWidget *treeview, const QStringList &strings, int type = Type)
QTreeWidgetItem(QTreeWidgetItem *parent, int type = Type)
QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &strings, int type = Type)
QTreeWidgetItem(const QTreeWidgetItem &other)
virtual ~QTreeWidgetItem()
virtual QTreeWidgetItem *clone() const
QTreeWidget *treeWidget() const
QTreeWidgetItem *parent() const
QTreeWidgetItem *child(int index) const
intchildCount() const
intindexOfChild(QTreeWidgetItem *child) const
intcolumnCount() const
voidaddChild(QTreeWidgetItem *child)
voidinsertChild(int index, QTreeWidgetItem *child)
voidremoveChild(QTreeWidgetItem *child)
QTreeWidgetItem *takeChild(int index)
voidaddChildren(const QList<QTreeWidgetItem *> &children)
voidinsertChildren(int index, const QList<QTreeWidgetItem *> &children)
QList<QTreeWidgetItem *>takeChildren()
Qt::ItemFlagsflags() const
voidsetFlags(Qt::ItemFlags flags)
boolisDisabled() const
voidsetDisabled(bool disabled)
boolisSelected() const
voidsetSelected(bool select)
boolisExpanded() const
voidsetExpanded(bool expand)
ChildIndicatorPolicychildIndicatorPolicy() const
voidsetChildIndicatorPolicy(ChildIndicatorPolicy policy)
QStringtext(int column) const
voidsetText(int column, const QString &text)
QStringtoolTip(int column) const
voidsetToolTip(int column, const QString &toolTip)
QFontfont(int column) const
voidsetFont(int column, const QFont &font)
inttextAlignment(int column) const
voidsetTextAlignment(int column, int alignment)
QBrushbackground(int column) const
voidsetBackground(int column, const QBrush &brush)
QBrushforeground(int column) const
voidsetForeground(int column, const QBrush &brush)
Qt::CheckStatecheckState(int column) const
voidsetCheckState(int column, Qt::CheckState state)
virtual QVariantdata(int column, int role) const
virtual voidsetData(int column, int role, const QVariant &value)
voidsortChildren(int column, Qt::SortOrder order)
virtual booloperator<(const QTreeWidgetItem &other) const
QTreeWidgetItem &operator=(const QTreeWidgetItem &other)
inttype() const

Detailed Description

An item is a row of a QTreeWidget: it has a text and other data for each column, and child items below it. An item that is constructed with a tree or with another item as its parent is added to it at once:

auto *fruit = new QTreeWidgetItem(tree, {"Fruit", "2"});
new QTreeWidgetItem(fruit, {"Apple", "red"});
new QTreeWidgetItem(fruit, {"Pear", "green"});
fruit->setExpanded(true);

setText(), setCheckState(), setFont(), setForeground() and their relatives set the data of a column, and setData() any role of it. setFlags() says what the user can do with the item: an item is edited in place only if it has Qt::ItemIsEditable. A parent owns its children, and the tree its top-level items: deleting an item deletes the items below it and takes it out of the tree.

Subclass the item to keep your own data with it, or to sort in your own way by reimplementing operator<().

See also QTreeWidget.

Differences from Qt

There is no icon, as Nt has no QIcon: give Qt::DecorationRole a QPixmap or a QColor with setData(). There are no statusTip(), whatsThis() and sizeHint() functions (use setData()), an item cannot be hidden or span the columns, and there are no stream operators. A check state is not propagated to the parent or the children by Qt::ItemIsAutoTristate.

Member Type Documentation

enum QTreeWidgetItem::ChildIndicatorPolicy

When an item has an arrow for opening it.

ConstantDescription
QTreeWidgetItem::ShowIndicatorAlways, also without children: for an item whose children are loaded when it is opened.
QTreeWidgetItem::DontShowIndicatorNever.
QTreeWidgetItem::DontShowIndicatorWhenChildlessOnly while it has children. This is the default.

enum QTreeWidgetItem::ItemType

The types of items, as type() returns them.

ConstantDescription
QTreeWidgetItem::TypeThe type of a plain item.
QTreeWidgetItem::UserTypeThe first number that a subclass can use for a type of its own.

Member Function Documentation

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

Constructs an item of the type type that is in no tree yet.

See also type().

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

This is an overloaded function.

Constructs an item of the type type with a text for each column from strings.

[explicit] QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *treeview, int type = Type)

This is an overloaded function.

Constructs an item of the type type and adds it to treeview as its last top-level item.

QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *treeview, const QStringList &strings, int type = Type)

This is an overloaded function.

Constructs an item of the type type with the texts strings and adds it to treeview as its last top-level item.

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

This is an overloaded function.

Constructs an item of the type type and adds it to parent as its last child.

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

This is an overloaded function.

Constructs an item of the type type with the texts strings and adds it to parent as its last child.

QTreeWidgetItem::QTreeWidgetItem(const QTreeWidgetItem &other)

Constructs a copy of other: its data, flags and type, but not its children, its parent or its tree.

See also clone().

[virtual] QTreeWidgetItem::~QTreeWidgetItem()

Destroys the item and its children, and takes it out of its parent or its tree.

void QTreeWidgetItem::addChild(QTreeWidgetItem *child)

Adds child as the last child of the item, which takes ownership of it. Nothing happens if child is nullptr or has a parent already.

See also insertChild() and takeChild().

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

Adds the items in children as the last children of the item.

See also addChild().

QBrush QTreeWidgetItem::background(int column) const

Returns the background of the item in the column column: an empty brush unless setBackground() set one.

See also setBackground().

Qt::CheckState QTreeWidgetItem::checkState(int column) const

Returns the check state of the item in the column column.

See also setCheckState().

QTreeWidgetItem * QTreeWidgetItem::child(int index) const

Returns the child at position index, or nullptr if there is none.

See also childCount() and indexOfChild().

int QTreeWidgetItem::childCount() const

Returns the number of children of the item.

ChildIndicatorPolicy QTreeWidgetItem::childIndicatorPolicy() const

Returns when the item has an arrow for opening it.

See also setChildIndicatorPolicy().

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

Returns a new item with a copy of the data of this item and copies of all the items below it. It is in no tree.

int QTreeWidgetItem::columnCount() const

Returns the number of columns that the item has data for.

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

Returns the data of the item in the column column for the role role, or an invalid QVariant if there is none. The edit role and the display role are the same data.

See also setData().

Qt::ItemFlags QTreeWidgetItem::flags() const

Returns what the user can do with the item. A new item is enabled, selectable and checkable.

See also setFlags().

QFont QTreeWidgetItem::font(int column) const

Returns the font of the text in the column column: a default font unless setFont() set one.

See also setFont().

QBrush QTreeWidgetItem::foreground(int column) const

Returns the brush that the text in the column column is drawn with: an empty brush unless setForeground() set one.

See also setForeground().

int QTreeWidgetItem::indexOfChild(QTreeWidgetItem *child) const

Returns the position of child among the children of the item, or -1 if it is not one of them.

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

Inserts child at position index of the children of the item, which takes ownership of it. Nothing happens if child is nullptr, has a parent or is in a tree already, or if index is out of range.

See also addChild().

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

Inserts the items in children at position index of the children of the item, in their order.

See also insertChild().

bool QTreeWidgetItem::isDisabled() const

Returns true if the item is not enabled: the user cannot select or change it.

See also setDisabled().

bool QTreeWidgetItem::isExpanded() const

Returns true if the item is expanded in its tree; otherwise returns false.

See also setExpanded().

bool QTreeWidgetItem::isSelected() const

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

See also setSelected().

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

Returns true if this item goes before other when items are sorted in ascending order. The data of the display role in the column that is sorted by is compared: as numbers if both are numbers, and as text otherwise. A subclass reimplements this to sort in its own way.

See also sortChildren() and QTreeWidget::sortItems().

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

Gives this item the data and the flags of other, and returns a reference to it. Its children, its parent, its tree and its type stay as they are.

QTreeWidgetItem * QTreeWidgetItem::parent() const

Returns the parent of the item, or nullptr for a top-level item and for an item that has none.

See also child().

void QTreeWidgetItem::removeChild(QTreeWidgetItem *child)

Removes child from the children of the item without deleting it: the caller owns it afterwards.

See also takeChild().

void QTreeWidgetItem::setBackground(int column, const QBrush &brush)

Sets the background of the item in the column column to brush.

See also background().

void QTreeWidgetItem::setCheckState(int column, Qt::CheckState state)

Sets the check state of the item in the column column to state, which gives the item a check box there. The user can toggle it if the item is Qt::ItemIsUserCheckable.

See also checkState().

void QTreeWidgetItem::setChildIndicatorPolicy(ChildIndicatorPolicy policy)

Sets when the item has an arrow for opening it to policy.

See also childIndicatorPolicy().

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

Sets the data of the item in the column column for the role role to value. The tree is told, and emits QTreeWidget::itemChanged(), if this changes anything. The item gets more columns if column is beyond the ones that it has, and so does its tree.

See also data().

void QTreeWidgetItem::setDisabled(bool disabled)

Disables the item if disabled is true, and enables it otherwise. Its children are not changed.

See also isDisabled() and setFlags().

void QTreeWidgetItem::setExpanded(bool expand)

Expands the item in its tree if expand is true, and collapses it otherwise. Nothing happens for an item that is in no tree, or that has no children.

See also isExpanded().

void QTreeWidgetItem::setFlags(Qt::ItemFlags flags)

Sets what the user can do with the item to flags. Add Qt::ItemIsEditable to let the user edit it:

item->setFlags(item->flags() | Qt::ItemIsEditable);

See also flags().

void QTreeWidgetItem::setFont(int column, const QFont &font)

Sets the font of the text in the column column to font.

See also font().

void QTreeWidgetItem::setForeground(int column, const QBrush &brush)

Sets the brush that the text in the column column is drawn with to brush.

See also foreground().

void QTreeWidgetItem::setSelected(bool select)

Selects the item in its tree if select is true, and deselects it otherwise. Nothing happens for an item that is in no tree.

See also isSelected().

void QTreeWidgetItem::setText(int column, const QString &text)

Sets the text of the item in the column column to text.

See also text().

void QTreeWidgetItem::setTextAlignment(int column, int alignment)

Sets the alignment of the text in the column column to alignment, a combination of Qt::AlignmentFlag values.

See also textAlignment().

void QTreeWidgetItem::setToolTip(int column, const QString &toolTip)

Sets the tool tip of the item in the column column to toolTip.

See also toolTip().

void QTreeWidgetItem::sortChildren(int column, Qt::SortOrder order)

Sorts the children of the item, and theirs, by the column column in the order order, with operator<().

See also QTreeWidget::sortItems().

QTreeWidgetItem * QTreeWidgetItem::takeChild(int index)

Removes the child at position index and returns it, or nullptr if there is none. The caller owns the item, which keeps its own children.

See also removeChild() and takeChildren().

QList<QTreeWidgetItem *> QTreeWidgetItem::takeChildren()

Removes all children of the item and returns them. The caller owns them.

See also takeChild().

QString QTreeWidgetItem::text(int column) const

Returns the text of the item in the column column.

See also setText().

int QTreeWidgetItem::textAlignment(int column) const

Returns the alignment of the text in the column column, a combination of Qt::AlignmentFlag values, or 0 if none was set.

See also setTextAlignment().

QString QTreeWidgetItem::toolTip(int column) const

Returns the tool tip of the item in the column column.

See also setToolTip().

QTreeWidget * QTreeWidgetItem::treeWidget() const

Returns the tree that the item is in, or nullptr.

int QTreeWidgetItem::type() const

Returns the type that was given to the constructor: Type for a plain item.