📄 qlistwidget.cpp
字号:
/*! \fn void QListWidget::itemEntered(QListWidgetItem *item) This signal is emitted when the mouse cursor enters an item. The \a 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.*//*! \fn void QListWidget::itemChanged(QListWidgetItem *item) This signal is emitted whenever the data of \a item has changed.*//*! \fn void QListWidget::currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous) This signal is emitted whenever the current item changes. The \a previous item is the item that previously had the focus, \a current is the new current item.*//*! \fn void QListWidget::currentTextChanged(const QString ¤tText) This signal is emitted whenever the current item changes. The \a currentText is the text data in the current item. If there is no current item, the \a currentText is invalid.*//*! \fn void QListWidget::currentRowChanged(int currentRow) This signal is emitted whenever the current item changes. The \a currentRow is the row of the current item. If there is no current item, the \a currentRow is -1.*//*! \fn void QListWidget::itemSelectionChanged() This signal is emitted whenever the selection changes. \sa selectedItems() isItemSelected() currentItemChanged()*//*! Constructs an empty QListWidget with the given \a parent.*/QListWidget::QListWidget(QWidget *parent) : QListView(*new QListWidgetPrivate(), parent){ Q_D(QListWidget); d->setup();}/*! Destroys the list widget and all its items.*/QListWidget::~QListWidget(){}/*! Returns the item that occupies the given \a row in the list if one has been set; otherwise returns 0. \sa row()*/QListWidgetItem *QListWidget::item(int row) const{ Q_D(const QListWidget); if (row < 0 || row >= d->model()->rowCount()) return 0; return d->model()->at(row);}/*! Returns the row containing the given \a item. \sa item()*/int QListWidget::row(const QListWidgetItem *item) const{ Q_D(const QListWidget); if (!item) return -1; return d->model()->index(const_cast<QListWidgetItem*>(item)).row();}/*! Inserts the \a item at the position in the list given by \a row. \sa addItem()*/void QListWidget::insertItem(int row, QListWidgetItem *item){ Q_D(QListWidget); d->model()->insert(row, item);}/*! Inserts an item with the text \a label in the list widget at the position given by \a row. \sa addItem()*/void QListWidget::insertItem(int row, const QString &label){ Q_D(QListWidget); d->model()->insert(row, new QListWidgetItem(label));}/*! Inserts items from the list of \a labels into the list, starting at the given \a row. \sa insertItem(), addItem()*/void QListWidget::insertItems(int row, const QStringList &labels){ Q_D(QListWidget); d->model()->insert(row, labels);}/*! Removes and returns the item from the given \a 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. \sa insertItem(), addItem()*/QListWidgetItem *QListWidget::takeItem(int row){ Q_D(QListWidget); if (row < 0 || row >= d->model()->rowCount()) return 0; return d->model()->take(row);}/*! \property QListWidget::count \brief the number of items in the list including any hidden items.*/int QListWidget::count() const{ Q_D(const QListWidget); return d->model()->rowCount();}/*! Returns the current item.*/QListWidgetItem *QListWidget::currentItem() const{ Q_D(const QListWidget); return d->model()->at(currentIndex().row());}/*! Sets the current item to \a item. Depending on the current selection mode, the item may also be selected.*/void QListWidget::setCurrentItem(QListWidgetItem *item){ Q_ASSERT(item); setCurrentRow(row(item));}/*! \property QListWidget::currentRow \brief the row of the current item. Depending on the current selection mode, the row may also be selected.*/int QListWidget::currentRow() const{ return currentIndex().row();}void QListWidget::setCurrentRow(int row){ Q_D(QListWidget); QModelIndex index = d->model()->index(row); if (d->selectionMode == SingleSelection) selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect); else if (d->selectionMode == NoSelection) selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate); else selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent);}/*! Returns a pointer to the item at the coordinates \a p.*/QListWidgetItem *QListWidget::itemAt(const QPoint &p) const{ Q_D(const QListWidget); QModelIndex index = indexAt(p); if (index.isValid()) return d->model()->at(index.row()); return 0;}/*! \fn QListWidgetItem *QListWidget::itemAt(int x, int y) const \overload Returns a pointer to the item at the coordinates (\a x, \a y).*//*! Returns the rectangle on the viewport occupied by the item at \a item.*/QRect QListWidget::visualItemRect(const QListWidgetItem *item) const{ Q_ASSERT(item); Q_D(const QListWidget); QModelIndex index = d->model()->index(const_cast<QListWidgetItem*>(item)); Q_ASSERT(index.isValid()); return visualRect(index);}/*! Sorts all the items in the list widget according to the specified \a order.*/void QListWidget::sortItems(Qt::SortOrder order){ Q_D(QListWidget); d->model()->sort(0, order);}/*! Starts editing the \a item if it is editable.*/void QListWidget::editItem(QListWidgetItem *item){ Q_ASSERT(item); Q_D(QListWidget); edit(d->model()->index(item));}/*! Opens an editor for the given \a item. The editor remains open after editing. \sa closePersistentEditor()*/void QListWidget::openPersistentEditor(QListWidgetItem *item){ Q_ASSERT(item); Q_D(QListWidget); QModelIndex index = d->model()->index(item); QAbstractItemView::openPersistentEditor(index);}/*! Closes the persistent editor for the given \a item. \sa openPersistentEditor()*/void QListWidget::closePersistentEditor(QListWidgetItem *item){ Q_ASSERT(item); Q_D(QListWidget); QModelIndex index = d->model()->index(item); QAbstractItemView::closePersistentEditor(index);}/*! \since 4.1 Returns the widget displayed in the given \a item.*/QWidget *QListWidget::itemWidget(QListWidgetItem *item) const{ Q_ASSERT(item); Q_D(const QListWidget); QModelIndex index = d->model()->index(item); return QAbstractItemView::indexWidget(index);}/*! \since 4.1 Sets the \a widget to be displayed in the give \a 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. \sa {Delegate Classes}*/void QListWidget::setItemWidget(QListWidgetItem *item, QWidget *widget){ Q_ASSERT(item); Q_D(QListWidget); QModelIndex index = d->model()->index(item); QAbstractItemView::setIndexWidget(index, widget);}/*! Returns true if \a item is selected; otherwise returns false.*/bool QListWidget::isItemSelected(const QListWidgetItem *item) const{ Q_D(const QListWidget); QModelIndex index = d->model()->index(const_cast<QListWidgetItem*>(item)); return selectionModel()->isSelected(index);}/*! Selects or deselects the given \a item depending on whether \a select is true of false.*/void QListWidget::setItemSelected(const QListWidgetItem *item, bool select){ Q_D(QListWidget); QModelIndex index = d->model()->index(const_cast<QListWidgetItem*>(item)); selectionModel()->select(index, select ? QItemSelectionModel::Select : QItemSelectionModel::Deselect);}/*! Returns a list of all selected items in the list widget.*/QList<QListWidgetItem*> QListWidget::selectedItems() const{ Q_D(const QListWidget); QModelIndexList indexes = selectionModel()->selectedIndexes(); QList<QListWidgetItem*> items; for (int i = 0; i < indexes.count(); ++i) items.append(d->model()->at(indexes.at(i).row())); return items;}/*! Finds items with the text that matches the string \a text using the given \a flags.*/QList<QListWidgetItem*> QListWidget::findItems(const QString &text, Qt::MatchFlags flags) const{ Q_D(const QListWidget); QModelIndexList indexes = d->model()->match(model()->index(0, 0, QModelIndex()), Qt::DisplayRole, text, -1, flags); QList<QListWidgetItem*> items; for (int i = 0; i < indexes.size(); ++i) items.append(d->model()->at(indexes.at(i).row())); return items;}/*! Returns true if the \a item is explicitly hidden; otherwise returns false.*/bool QListWidget::isItemHidden(const QListWidgetItem *item) const{ return isRowHidden(row(item));}/*! If \a hide is true, the \a item will be hidden; otherwise it will be shown.*/void QListWidget::setItemHidden(const QListWidgetItem *item, bool hide){ setRowHidden(row(item), hide);}/*! Scrolls the view if necessary to ensure that the \a item is visible. The \a hint parameter specifies more precisely where the \a item should be located after the operation.*/void QListWidget::scrollToItem(const QListWidgetItem *item, QAbstractItemView::ScrollHint hint){ Q_ASSERT(item); Q_D(QListWidget); QModelIndex index = d->model()->index(const_cast<QListWidgetItem*>(item)); Q_ASSERT(index.isValid()); QListView::scrollTo(index, hint);}/*! Removes all items and selections in the view.*/void QListWidget::clear(){ Q_D(QListWidget); selectionModel()->clear(); d->model()->clear();}/*! Returns a list of MIME types that can be used to describe a list of listwidget items. \sa mimeData()*/QStringList QListWidget::mimeTypes() const{ return d_func()->model()->QAbstractListModel::mimeTypes();}/*! Returns an object that contains a serialized description of the specified \a items. The format used to describe the items is obtained from the mimeTypes() function. If the list of items is empty, 0 is returned rather than a serialized empty list.*/QMimeData *QListWidget::mimeData(const QList<QListWidgetItem*>) const{ return d_func()->model()->internalMimeData();}#ifndef QT_NO_DRAGANDDROP/*! Handles the \a data supplied by a drag and drop operation that ended with the given \a action in the given \a index. \sa supportedDropActions()*/bool QListWidget::dropMimeData(int index, const QMimeData *data, Qt::DropAction action){ QModelIndex idx; int row = index; int column = 0; if (dropIndicatorPosition() == QAbstractItemView::OnItem) { // QAbstractListModel::dropMimeData will overwrite on the index if row == -1 and column == -1 idx = model()->index(row, column); row = -1; column = -1; } return d_func()->model()->QAbstractListModel::dropMimeData(data, action , row, column, idx);}/*! Returns the drop actions supported by this view. \sa Qt::DropActions*/Qt::DropActions QListWidget::supportedDropActions() const{ return d_func()->model()->QAbstractListModel::supportedDropActions();}#endif // QT_NO_DRAGANDDROP/*! Returns a list of pointers to the items contained in the \a data object. If the object was not created by a QListWidget in the same process, the list is empty.*/QList<QListWidgetItem*> QListWidget::items(const QMimeData *data) const{ const QListWidgetMimeData *lwd = qobject_cast<const QListWidgetMimeData*>(data); if (lwd) return lwd->items; return QList<QListWidgetItem*>();}/*! Returns the QModelIndex assocated with the given \a item.*/QModelIndex QListWidget::indexFromItem(QListWidgetItem *item) const{ Q_D(const QListWidget); Q_ASSERT(item); return d->model()->index(item);}/*! Returns a pointer to the QListWidgetItem assocated with the given \a index.*/QListWidgetItem *QListWidget::itemFromIndex(const QModelIndex &index) const{ Q_D(const QListWidget); Q_ASSERT(index.isValid()); return d->model()->at(index.row());}/*! \internal*/void QListWidget::setModel(QAbstractItemModel * /*model*/){ qFatal("QListWidget::setModel() - Changing the model of the QListWidget is not allowed.");}/* \reimp */bool QListWidget::event(QEvent *e){ return QListView::event(e);}#include "moc_qlistwidget.cpp"#endif // QT_NO_LISTWIDGET
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -