QListWidget Class
The QListWidget class provides an item-based list widget. 更多...
头文件: | #include <QListWidget> |
qmake: | QT += widgets |
基类: | QListView |
属性
- count : const int
- currentRow : int
- sortingEnabled : bool
- 13 个属性继承自 QListView
- 16 个属性继承自 QAbstractItemView
- 3 个属性继承自 QAbstractScrollArea
- 6 个属性继承自 QFrame
- 59 个属性继承自 QWidget
- 1 个属性继承自 QObject
公有函数
QListWidget(QWidget *parent = Q_NULLPTR) | |
~QListWidget() | |
void | addItem(const QString &label) |
void | addItem(QListWidgetItem *item) |
void | addItems(const QStringList &labels) |
void | closePersistentEditor(QListWidgetItem *item) |
int | count() const |
QListWidgetItem * | currentItem() const |
int | currentRow() const |
void | editItem(QListWidgetItem *item) |
QList<QListWidgetItem *> | findItems(const QString &text, Qt::MatchFlags flags) const |
void | insertItem(int row, QListWidgetItem *item) |
void | insertItem(int row, const QString &label) |
void | insertItems(int row, const QStringList &labels) |
bool | isSortingEnabled() const |
QListWidgetItem * | item(int row) const |
QListWidgetItem * | itemAt(const QPoint &p) const |
QListWidgetItem * | itemAt(int x, int y) const |
QWidget * | itemWidget(QListWidgetItem *item) const |
void | openPersistentEditor(QListWidgetItem *item) |
void | removeItemWidget(QListWidgetItem *item) |
int | row(const QListWidgetItem *item) const |
QList<QListWidgetItem *> | selectedItems() const |
void | setCurrentItem(QListWidgetItem *item) |
void | setCurrentItem(QListWidgetItem *item, QItemSelectionModel::SelectionFlags command) |
void | setCurrentRow(int row) |
void | setCurrentRow(int row, QItemSelectionModel::SelectionFlags command) |
void | setItemWidget(QListWidgetItem *item, QWidget *widget) |
void | setSortingEnabled(bool enable) |
void | sortItems(Qt::SortOrder order = Qt::AscendingOrder) |
QListWidgetItem * | takeItem(int row) |
QRect | visualItemRect(const QListWidgetItem *item) const |
重新实现的公有函数
virtual void | dropEvent(QDropEvent *event) |
virtual void | setSelectionModel(QItemSelectionModel *selectionModel) |
- 32 个公有函数继承自 QListView
- 59 个公有函数继承自 QAbstractItemView
- 20 个公有函数继承自 QAbstractScrollArea
- 14 个公有函数继承自 QFrame
- 214 个公有函数继承自 QWidget
- 32 个公有函数继承自 QObject
- 14 个公有函数继承自 QPaintDevice
公有槽函数
void | clear() |
void | scrollToItem(const QListWidgetItem *item, QAbstractItemView::ScrollHint hint = EnsureVisible) |
- 9 个公有槽函数继承自 QAbstractItemView
- 19 个公有槽函数继承自 QWidget
- 1 个公有槽函数继承自 QObject
信号
void | currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous) |
void | currentRowChanged(int currentRow) |
void | currentTextChanged(const QString ¤tText) |
void | itemActivated(QListWidgetItem *item) |
void | itemChanged(QListWidgetItem *item) |
void | itemClicked(QListWidgetItem *item) |
void | itemDoubleClicked(QListWidgetItem *item) |
void | itemEntered(QListWidgetItem *item) |
void | itemPressed(QListWidgetItem *item) |
void | itemSelectionChanged() |
- 1 个信号继承自 QListView
- 7 个信号继承自 QAbstractItemView
- 3 个信号继承自 QWidget
- 2 个信号继承自 QObject
受保护的函数
virtual bool | dropMimeData(int index, const QMimeData *data, Qt::DropAction action) |
QModelIndex | indexFromItem(QListWidgetItem *item) const |
QListWidgetItem * | itemFromIndex(const QModelIndex &index) const |
QList<QListWidgetItem *> | items(const QMimeData *data) const |
virtual QMimeData * | mimeData(const QList<QListWidgetItem *> items) const |
virtual QStringList | mimeTypes() const |
virtual Qt::DropActions | supportedDropActions() const |
重新实现的受保护函数
virtual bool | event(QEvent *e) |
- 28 个受保护的函数继承自 QListView
- 37 个受保护的函数继承自 QAbstractItemView
- 20 个受保护的函数继承自 QAbstractScrollArea
- 4 个受保护的函数继承自 QFrame
- 35 个受保护的函数继承自 QWidget
- 9 个受保护的函数继承自 QObject
- 1 个受保护的函数继承自 QPaintDevice
其他继承的成员
- 1 个公有变量继承自 QObject
- 5 个静态公有成员继承自 QWidget
- 10 个静态公有成员继承自 QObject
- 9 个受保护的槽函数继承自 QAbstractItemView
- 1 个受保护的槽函数继承自 QWidget
- 2 个受保护的变量继承自 QObject
- 1 protected type inherited from QPaintDevice
详细描述
The QListWidget class provides an item-based list widget.
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.
![]() | ![]() | ![]() |
A Windows Vista style list widget. | A Macintosh style list widget. | A Fusion style list widget. |
参见 QListWidgetItem, QListView, QTreeView, Model/View Programming, and Config Dialog Example.
属性
count : const int
This property holds the number of items in the list including any hidden items.
访问函数:
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.
访问函数:
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.
访问函数:
bool | isSortingEnabled() const |
void | setSortingEnabled(bool enable) |
成员函数
QListWidget::QListWidget(QWidget *parent = Q_NULLPTR)
Constructs an empty QListWidget with the given parent.
QListWidget::~QListWidget()
Destroys the list widget and all its items.
void QListWidget::addItem(const QString &label)
Inserts an item with the text label at the end of the list widget.
void QListWidget::addItem(QListWidgetItem *item)
Inserts the item at the end of the list widget.
Warning: A QListWidgetItem can only be added to a QListWidget once. Adding the same QListWidgetItem multiple times to a QListWidget will result in undefined behavior.
参见 insertItem().
void QListWidget::addItems(const QStringList &labels)
Inserts items with the text labels at the end of the list widget.
参见 insertItems().
[slot]
void QListWidget::clear()
Removes all items and selections in the view.
Warning: All items will be permanently deleted.
void QListWidget::closePersistentEditor(QListWidgetItem *item)
Closes the persistent editor for the given item.
参见 openPersistentEditor().
QListWidgetItem *QListWidget::currentItem() const
Returns the current item.
参见 setCurrentItem().
[signal]
void QListWidget::currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
This signal is emitted whenever the current item changes.
previous is the item that previously had the focus; current is the new current item.
[signal]
void QListWidget::currentRowChanged(int currentRow)
This signal is emitted whenever the current item changes.
currentRow is the row of the current item. If there is no current item, the currentRow is -1.
Note: Notifier signal for property currentRow.
[signal]
void QListWidget::currentTextChanged(const QString ¤tText)
This signal is emitted whenever the current item changes.
currentText is the text data in the current item. If there is no current item, the currentText is invalid.
[virtual]
void QListWidget::dropEvent(QDropEvent *event)
重新实现 QWidget::dropEvent().
[virtual protected]
bool QListWidget::dropMimeData(int index, const QMimeData *data, Qt::DropAction action)
Handles data supplied by an external drag and drop operation that ended with the given action in the given index. Returns true
if data and action can be handled by the model; otherwise returns false
.
参见 supportedDropActions().
void QListWidget::editItem(QListWidgetItem *item)
Starts editing the item if it is editable.
[virtual protected]
bool QListWidget::event(QEvent *e)
重新实现 QObject::event().
QList<QListWidgetItem *> QListWidget::findItems(const QString &text, Qt::MatchFlags flags) const
Finds items with the text that matches the string text using the given flags.
[protected]
QModelIndex QListWidget::indexFromItem(QListWidgetItem *item) const
Returns the QModelIndex associated with the given item.
void QListWidget::insertItem(int row, QListWidgetItem *item)
Inserts the item at the position in the list given by row.
参见 addItem().
void QListWidget::insertItem(int row, const QString &label)
Inserts an item with the text label in the list widget at the position given by row.
参见 addItem().
void QListWidget::insertItems(int row, const QStringList &labels)
Inserts items from the list of labels into the list, starting at the given row.
参见 insertItem() and addItem().
QListWidgetItem *QListWidget::item(int row) const
Returns the item that occupies the given row in the list if one has been set; otherwise returns 0.
参见 row().
[signal]
void QListWidget::itemActivated(QListWidgetItem *item)
This signal is emitted when the item is activated. The item is activated when the user clicks or double clicks on it, depending on the system configuration. It is also activated when the user presses the activation key (on Windows and X11 this is the Return key, on Mac OS X it is Command+O).
QListWidgetItem *QListWidget::itemAt(const QPoint &p) const
Returns a pointer to the item at the coordinates p. The coordinates are relative to the list widget's viewport().
QListWidgetItem *QListWidget::itemAt(int x, int y) const
This is an overloaded function.
Returns a pointer to the item at the coordinates (x, y). The coordinates are relative to the list widget's viewport().
[signal]
void QListWidget::itemChanged(QListWidgetItem *item)
This signal is emitted whenever the data of item has changed.
[signal]
void QListWidget::itemClicked(QListWidgetItem *item)
This signal is emitted with the specified item when a mouse button is clicked on an item in the widget.
参见 itemPressed() and itemDoubleClicked().
[signal]
void QListWidget::itemDoubleClicked(QListWidgetItem *item)
This signal is emitted with the specified item when a mouse button is double clicked on an item in the widget.
参见 itemClicked() and itemPressed().
[signal]
void QListWidget::itemEntered(QListWidgetItem *item)
This signal is emitted when the mouse cursor enters an item. The item is the item entered. This signal is only emitted when mouseTracking is turned on, or when a mouse button is pressed while moving into an item.
参见 QWidget::setMouseTracking().
[protected]
QListWidgetItem *QListWidget::itemFromIndex(const QModelIndex &index) const
Returns a pointer to the QListWidgetItem associated with the given index.
[signal]
void QListWidget::itemPressed(QListWidgetItem *item)
This signal is emitted with the specified item when a mouse button is pressed on an item in the widget.
参见 itemClicked() and itemDoubleClicked().
[signal]
void QListWidget::itemSelectionChanged()
This signal is emitted whenever the selection changes.
参见 selectedItems(), QListWidgetItem::isSelected(), and currentItemChanged().
QWidget *QListWidget::itemWidget(QListWidgetItem *item) const
Returns the widget displayed in the given item.
This function was introduced in Qt 4.1.
参见 setItemWidget() and removeItemWidget().
[protected]
QList<QListWidgetItem *> QListWidget::items(const QMimeData *data) const
Returns a list of pointers to the items contained in the data object. If the object was not created by a QListWidget in the same process, the list is empty.
[virtual protected]
QMimeData *QListWidget::mimeData(const QList<QListWidgetItem *> items) const
Returns an object that contains a serialized description of the specified items. The format used to describe the items is obtained from the mimeTypes() function.
If the list of items is empty, 0 is returned instead of a serialized empty list.
[virtual protected]
QStringList QListWidget::mimeTypes() const
Returns a list of MIME types that can be used to describe a list of listwidget items.
参见 mimeData().
void QListWidget::openPersistentEditor(QListWidgetItem *item)
Opens an editor for the given item. The editor remains open after editing.
参见 closePersistentEditor().
void QListWidget::removeItemWidget(QListWidgetItem *item)
Removes the widget set on the given item.
To remove an item (row) from the list entirely, either delete the item or use takeItem().
This function was introduced in Qt 4.3.
参见 itemWidget() and setItemWidget().
int QListWidget::row(const QListWidgetItem *item) const
Returns the row containing the given item.
参见 item().
[slot]
void QListWidget::scrollToItem(const QListWidgetItem *item, QAbstractItemView::ScrollHint hint = EnsureVisible)
Scrolls the view if necessary to ensure that the item is visible.
hint specifies where the item should be located after the operation.
QList<QListWidgetItem *> QListWidget::selectedItems() const
Returns a list of all selected items in the list widget.
void QListWidget::setCurrentItem(QListWidgetItem *item)
Sets the current item to item.
Unless the selection mode is NoSelection, the item is also selected.
参见 currentItem().
void QListWidget::setCurrentItem(QListWidgetItem *item, QItemSelectionModel::SelectionFlags command)
Set the current item to item, using the given command.
This function was introduced in Qt 4.4.
void QListWidget::setCurrentRow(int row, QItemSelectionModel::SelectionFlags command)
Sets the current row to be the given row, using the given command,
This function was introduced in Qt 4.4.
Note: Setter function for property currentRow.
void QListWidget::setItemWidget(QListWidgetItem *item, QWidget *widget)
Sets the widget to be displayed in the given item.
This function should only be used to display static content in the place of a list widget item. If you want to display custom dynamic content or implement a custom editor widget, use QListView and subclass QItemDelegate instead.
This function was introduced in Qt 4.1.
参见 itemWidget(), removeItemWidget(), and Delegate Classes.
[virtual]
void QListWidget::setSelectionModel(QItemSelectionModel *selectionModel)
重新实现 QAbstractItemView::setSelectionModel().
void QListWidget::sortItems(Qt::SortOrder order = Qt::AscendingOrder)
Sorts all the items in the list widget according to the specified order.
[virtual protected]
Qt::DropActions QListWidget::supportedDropActions() const
Returns the drop actions supported by this view.
参见 Qt::DropActions.
QListWidgetItem *QListWidget::takeItem(int row)
Removes and returns the item from the given row in the list widget; otherwise returns 0.
Items removed from a list widget will not be managed by Qt, and will need to be deleted manually.
参见 insertItem() and addItem().
QRect QListWidget::visualItemRect(const QListWidgetItem *item) const
Returns the rectangle on the viewport occupied by the item at item.