QListWidgetItem Class

The QListWidgetItem class provides an item for use with the QListWidget item view class. More...

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

Public Types

enum ItemType { Type, UserType }

Public Functions

QListWidgetItem(const QListWidgetItem &other)
virtual ~QListWidgetItem()
virtual QListWidgetItem *clone() const
virtual QVariant data(int role) const
virtual void read(QDataStream &in)
virtual void setData(int role, const QVariant &value)
virtual void write(QDataStream &out) const
virtual bool operator<(const QListWidgetItem &other) const
QListWidgetItem &operator=(const QListWidgetItem &other)
QDataStream &operator<<(QDataStream &out, const QListWidgetItem &item)
QDataStream &operator>>(QDataStream &in, QListWidgetItem &item)

Detailed Description

A QListWidgetItem represents a single item in a QListWidget. Each item can hold several pieces of information, and will display them appropriately.

The item view convenience classes use a classic item-based interface rather than a pure model/view approach. For a more flexible list view widget, consider using the QListView class with a standard model.

List items can be inserted automatically into a list, when they are constructed, by specifying the list widget:

     new QListWidgetItem(tr("Hazel"), listWidget);

Alternatively, list items can also be created without a parent widget, and later inserted into a list using QListWidget::insertItem().

List items are typically used to display text() and an icon(). These are set with the setText() and setIcon() functions. The appearance of the text can be customized with setFont(), setForeground(), and setBackground(). Text in list items can be aligned using the setTextAlignment() function. Tooltips, status tips and "What's This?" help can be added to list items with setToolTip(), setStatusTip(), and setWhatsThis().

By default, items are enabled, selectable, checkable, and can be the source of drag and drop operations.

Each item's flags can be changed by calling setFlags() with the appropriate value (see Qt::ItemFlags). Checkable items can be checked, unchecked and partially checked with the setCheckState() function. The corresponding checkState() function indicates the item's current check state.

The isHidden() function can be used to determine whether the item is hidden. To hide an item, use setHidden().

Subclassing

When subclassing QListWidgetItem to provide custom items, it is possible to define new types for them enabling them to be distinguished from standard items. For subclasses that require this feature, ensure that you call the base class constructor with a new type value equal to or greater than UserType, within your constructor.

See also QListWidget, Model/View Programming, QTreeWidgetItem, and QTableWidgetItem.

Member Type Documentation

enum QListWidgetItem::ItemType

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

ConstantValueDescription
QListWidgetItem::Type0The default type for list widget items.
QListWidgetItem::UserType1000The minimum value for custom types. Values below UserType are reserved by Qt.

You can define new user types in QListWidgetItem subclasses to ensure that custom items are treated specially.

See also type().

Member Function Documentation

QListWidgetItem::QListWidgetItem(const QListWidgetItem &other)

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

This function is useful when reimplementing clone().

See also data() and flags().

[virtual noexcept] QListWidgetItem::~QListWidgetItem()

Destroys the list item.

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

Creates an exact copy of the item.

[virtual] QVariant QListWidgetItem::data(int role) const

Returns the item's data for a given role. Reimplement this function if you need extra roles or special behavior for certain roles.

See also Qt::ItemDataRole and setData().

[virtual] void QListWidgetItem::read(QDataStream &in)

Reads the item from stream in.

See also write().

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

Sets the data for a given role to the given value. Reimplement this function if you need extra roles or special behavior for certain roles.

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

See also Qt::ItemDataRole and data().

[virtual] void QListWidgetItem::write(QDataStream &out) const

Writes the item to stream out.

See also read().

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

Returns true if this item's text is less then other item's text; otherwise returns false.

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

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

This function is useful when reimplementing clone().

See also data() and flags().

Related Non-Members

QDataStream &operator<<(QDataStream &out, const QListWidgetItem &item)

Writes the list widget item item to stream out.

This operator uses QListWidgetItem::write().

See also Serializing Qt Data Types.

QDataStream &operator>>(QDataStream &in, QListWidgetItem &item)

Reads a list widget item from stream in into item.

This operator uses QListWidgetItem::read().

See also Serializing Qt Data Types.