QListWidget Class

The QListWidget class provides an item-based list widget. More...

Header: #include <QListWidget>
qmake: QT += widgets
Inherits: QListView

Properties

Public Functions

int count() const
int currentRow() const
bool isSortingEnabled() const
void setCurrentRow(int row)
void setCurrentRow(int row, QItemSelectionModel::SelectionFlags command)
void setSortingEnabled(bool enable)

Signals

void currentRowChanged(int currentRow)

Detailed Description

QListWidget is a convenience class that provides a list view similar to the one supplied by QListView, but with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list.

For a more flexible list view widget, use the QListView class with a standard model.

List widgets are constructed in the same way as other widgets:

     QListWidget *listWidget = new QListWidget(this);

The selectionMode() of a list widget determines how many of the items in the list can be selected at the same time, and whether complex selections of items can be created. This can be set with the setSelectionMode() function.

There are two ways to add items to the list: they can be constructed with the list widget as their parent widget, or they can be constructed with no parent widget and added to the list later. If a list widget already exists when the items are constructed, the first method is easier to use:

     new QListWidgetItem(tr("Oak"), listWidget);
     new QListWidgetItem(tr("Fir"), listWidget);
     new QListWidgetItem(tr("Pine"), listWidget);

If you need to insert a new item into the list at a particular position, then it should be constructed without a parent widget. The insertItem() function should then be used to place it within the list. The list widget will take ownership of the item.

     QListWidgetItem *newItem = new QListWidgetItem;
     newItem->setText(itemText);
     listWidget->insertItem(row, newItem);

For multiple items, insertItems() can be used instead. The number of items in the list is found with the count() function. To remove items from the list, use takeItem().

The current item in the list can be found with currentItem(), and changed with setCurrentItem(). The user can also change the current item by navigating with the keyboard or clicking on a different item. When the current item changes, the currentItemChanged() signal is emitted with the new current item and the item that was previously current.

See also QListWidgetItem, QListView, QTreeView, Model/View Programming, and Tab Dialog Example.

Property Documentation

count : const int

This property holds the number of items in the list including any hidden items.

Access functions:

int count() const

currentRow : int

This property holds the row of the current item.

Depending on the current selection mode, the row may also be selected.

Access functions:

int currentRow() const
void setCurrentRow(int row)
void setCurrentRow(int row, QItemSelectionModel::SelectionFlags command)

Notifier signal:

void currentRowChanged(int currentRow)

sortingEnabled : bool

This property holds whether sorting is enabled

If this property is true, sorting is enabled for the list; if the property is false, sorting is not enabled.

The default value is false.

This property was introduced in Qt 4.2.

Access functions:

bool isSortingEnabled() const
void setSortingEnabled(bool enable)