📄 qabstractitemview.cpp
字号:
QAbstractItemView::QAbstractItemView(QAbstractItemViewPrivate &dd, QWidget *parent) : QAbstractScrollArea(dd, parent){ d_func()->init();}/*! Destroys the view.*/QAbstractItemView::~QAbstractItemView(){}/*! Sets the \a model for the view to present. \bold{Note:} This function will also create and set a new selection model, replacing any previously set with setSelectionModel(). \sa selectionModel(), setSelectionModel()*/void QAbstractItemView::setModel(QAbstractItemModel *model){ Q_D(QAbstractItemView); if (d->model) { disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(dataChanged(QModelIndex,QModelIndex))); disconnect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(rowsInserted(QModelIndex,int,int))); disconnect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(rowsAboutToBeRemoved(QModelIndex,int,int))); disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(_q_rowsRemoved(QModelIndex,int,int))); disconnect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int))); disconnect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(_q_columnsRemoved(QModelIndex,int,int))); disconnect(d->model, SIGNAL(modelReset()), this, SLOT(reset())); disconnect(d->model, SIGNAL(layoutChanged()), this, SLOT(doItemsLayout())); } d->model = model; if (d->model) { // These asserts do basic sanity checking of the model // A model should return the same index, including its internal id/pointer. Q_ASSERT(model->index(0,0) == model->index(0,0)); // The parent of a top level index should be invalid. Q_ASSERT(model->index(0,0).parent() == QModelIndex()); connect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(dataChanged(QModelIndex,QModelIndex))); connect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(rowsInserted(QModelIndex,int,int))); connect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(rowsAboutToBeRemoved(QModelIndex,int,int))); connect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(_q_rowsRemoved(QModelIndex,int,int))); connect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int))); connect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(_q_columnsRemoved(QModelIndex,int,int))); connect(d->model, SIGNAL(modelReset()), this, SLOT(reset())); connect(d->model, SIGNAL(layoutChanged()), this, SLOT(doItemsLayout())); } setSelectionModel(new QItemSelectionModel(d->model, this)); reset(); // kill editors, set new root and do layout}/*! Returns the model that this view is presenting.*/QAbstractItemModel *QAbstractItemView::model() const{ return d_func()->model;}/*! Sets the current selection to the given \a selectionModel. Note that, if you call setModel() after this function, the given \a selectionModel will be replaced by a one created by the view. \sa selectionModel(), setModel(), clearSelection()*/void QAbstractItemView::setSelectionModel(QItemSelectionModel *selectionModel){ Q_ASSERT(selectionModel); Q_D(QAbstractItemView); if (selectionModel->model() != model()) { qWarning("QAbstractItemView::setSelectionModel() failed: " "Trying to set a selection model, which works on " "a different model than the view."); return; } if (d->selectionModel) { disconnect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(selectionChanged(QItemSelection,QItemSelection))); disconnect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(currentChanged(QModelIndex,QModelIndex))); } d->selectionModel = selectionModel; if (d->selectionModel) { connect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(selectionChanged(QItemSelection,QItemSelection))); connect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(currentChanged(QModelIndex,QModelIndex))); }}/*! Returns the current selection. \sa setSelectionModel(), clearSelection()*/QItemSelectionModel* QAbstractItemView::selectionModel() const{ return d_func()->selectionModel;}/*! Sets the item delegate for this view and its model to \a delegate. This is useful if you want complete control over the editing and display of items. \sa itemDelegate()*/void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate){ Q_ASSERT(delegate); Q_D(QAbstractItemView); if (d->delegate) { disconnect(d->delegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)), this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint))); disconnect(d->delegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*))); } d->delegate = delegate; if (d->delegate) { connect(d->delegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)), this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint))); connect(d->delegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*))); }}/*! Returns the item delegate used by this view and model. This is either one set with setItemDelegate(), or the default one. \sa setItemDelegate()*/QAbstractItemDelegate *QAbstractItemView::itemDelegate() const{ return d_func()->delegate;}/*! \property QAbstractItemView::selectionMode \brief which selection mode the view operates in This property controls whether the user can select one or many items and, in many-item selections, whether the selection must be a continuous range of items. \sa SelectionMode SelectionBehavior*/void QAbstractItemView::setSelectionMode(SelectionMode mode){ d_func()->selectionMode = mode;}QAbstractItemView::SelectionMode QAbstractItemView::selectionMode() const{ return d_func()->selectionMode;}/*! \property QAbstractItemView::selectionBehavior \brief which selection behavior the view uses This property holds whether selections are done in terms of single items, rows or columns. \sa SelectionMode SelectionBehavior*/void QAbstractItemView::setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior){ d_func()->selectionBehavior = behavior;}QAbstractItemView::SelectionBehavior QAbstractItemView::selectionBehavior() const{ return d_func()->selectionBehavior;}/*! Sets the current item to be the item at \a index. Depending on the current selection mode, the item may also be selected. To set an item as the current item without selecting it, call \c{selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);} \sa currentIndex(), setSelectionMode(), selectionMode()*/void QAbstractItemView::setCurrentIndex(const QModelIndex &index){ if (selectionModel()) selectionModel()->setCurrentIndex(index, selectionCommand(index, 0));}/*! Returns the model index of the current item. \sa setCurrentIndex()*/QModelIndex QAbstractItemView::currentIndex() const{ return selectionModel() ? selectionModel()->currentIndex() : QModelIndex();}/*! Reset the internal state of the view.*/void QAbstractItemView::reset(){ Q_D(QAbstractItemView); _q_abstractitemview_editor_const_iterator it = d->editors.begin(); for (; it != d->editors.end(); ++it) d->releaseEditor(d->editorForIterator(it)); d->editors.clear(); d->persistent.clear(); setState(NoState); setRootIndex(QModelIndex());}/*! Sets the root item to the item at the given \a index. \sa rootIndex()*/void QAbstractItemView::setRootIndex(const QModelIndex &index){ Q_D(QAbstractItemView); d->root = index; d->doDelayedItemsLayout();}/*! Returns the model index of the model's root item. The root item is the parent item to the views toplevel items. The root can be invalid. \sa setRootIndex()*/QModelIndex QAbstractItemView::rootIndex() const{ return QModelIndex(d_func()->root);}/*! Selects all non-hidden items.*/void QAbstractItemView::selectAll(){ if (!model() || !selectionModel()) return; QItemSelection selection; QModelIndex tl = model()->index(0, 0, rootIndex()); QModelIndex br = model()->index(model()->rowCount(rootIndex()) - 1, model()->columnCount(rootIndex()) - 1, rootIndex()); selection.append(QItemSelectionRange(tl, br)); selectionModel()->select(selection, QItemSelectionModel::ClearAndSelect);}/*! Starts editing the item item at \a index if it is editable.*/void QAbstractItemView::edit(const QModelIndex &index){ if (!index.isValid()) qWarning("edit: index was invalid"); if (!edit(index, AllEditTriggers, 0)) qWarning("edit: editing failed");}/*! Clears all selected items and the current index.*/void QAbstractItemView::clearSelection(){ if (selectionModel()) selectionModel()->clear();}/*! \internal This function is intended to lay out the items in the view. The default implementatiidon just calls updateGeometries() and updates the viewport.*/void QAbstractItemView::doItemsLayout(){ d_func()->delayedLayout.stop(); updateGeometries(); d_func()->viewport->update();}/*! \property QAbstractItemView::editTriggers \brief which actions will initiate item editing This property is a selection of flags defined by \l{EditTrigger}, combined using the OR operator. The view will only initiate the editing of an item if the action performed is set in this property.*/void QAbstractItemView::setEditTriggers(EditTriggers actions){ d_func()->editTriggers = actions;}QAbstractItemView::EditTriggers QAbstractItemView::editTriggers() const{ return d_func()->editTriggers;}/*! \property QAbstractItemView::autoScroll \brief whether autoscrolling in drag move events is enabled If this property is set to true (the default), the QAbstractItemView automatically scrolls the contents of the view if the user drags within 16 pixels of the viewport edge. This only works if the viewport accepts drops. Autoscroll is switched off by setting this property to false.*/void QAbstractItemView::setAutoScroll(bool enable){ d_func()->autoScroll = enable;}bool QAbstractItemView::hasAutoScroll() const{ return d_func()->autoScroll;}/*! \property QAbstractItemView::tabKeyNavigation \brief whether item navigation with tab and backtab is enabled.*/void QAbstractItemView::setTabKeyNavigation(bool enable){ d_func()->tabKeyNavigation = enable;}bool QAbstractItemView::tabKeyNavigation() const{ return d_func()->tabKeyNavigation;}#ifndef QT_NO_DRAGANDDROP/*! \property QAbstractItemView::showDropIndicator \brief whether the drop indicator is shown when dragging items and dropping.*/void QAbstractItemView::setDropIndicatorShown(bool enable){ d_func()->showDropIndicator = enable;}bool QAbstractItemView::showDropIndicator() const{ return d_func()->showDropIndicator;}/*! \property QAbstractItemView::dragEnabled \brief whether the view supports dragging of its own items*/void QAbstractItemView::setDragEnabled(bool enable){ d_func()->dragEnabled = enable;}bool QAbstractItemView::dragEnabled() const{ return d_func()->dragEnabled;}#endif // QT_NO_DRAGANDDROP/*! \property QAbstractItemView::alternatingRowColors \brief whether to draw the background using alternating colors If this property is true, the item background will be drawn using QPalette::Base and QPalette::AlternateBase; otherwise the background will be drawn using the QPalette::Base color.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -