📄 qitemselectionmodel.cpp
字号:
Merge the selected indexes into selection ranges again.*/void QItemSelectionModelPrivate::_q_layoutChanged(){ if (savedPersistentCurrentIndexes.isEmpty() && savedPersistentIndexes.isEmpty()) { // either the selection was actually empty, or we // didn't get the layoutAboutToBeChanged() signal return; } // clear the "old" selection ranges.clear(); currentSelection.clear(); // sort the "new" selection, as preparation for merging qStableSort(savedPersistentIndexes.begin(), savedPersistentIndexes.end()); qStableSort(savedPersistentCurrentIndexes.begin(), savedPersistentCurrentIndexes.end()); // update the selection by merging the individual indexes ranges = mergeIndexes(savedPersistentIndexes); currentSelection = mergeIndexes(savedPersistentCurrentIndexes); // release the persistent indexes savedPersistentIndexes.clear(); savedPersistentCurrentIndexes.clear();}/*! \class QItemSelectionModel \brief The QItemSelectionModel class keeps track of a view's selected items. \ingroup model-view A QItemSelectionModel keeps track of the selected items in a view, or in several views onto the same model. It also keeps track of the currently selected item in a view. The QItemSelectionModel class is one of the \l{Model/View Classes} and is part of Qt's \l{Model/View Programming}{model/view framework}. The selected items are stored using ranges. Whenever you want to modify the selected items use select() and provide either a QItemSelection, or a QModelIndex and a QItemSelectionModel::SelectionFlag. The QItemSelectionModel takes a two layer approach to selection management, dealing with both selected items that have been committed and items that are part of the current selection. The current selected items are part of the current interactive selection (for example with rubber-band selection or keyboard-shift selections). To update the currently selected items, use the bitwise OR of QItemSelectionModel::Current and any of the other SelectionFlags. If you omit the QItemSelectionModel::Current command, a new current selection will be created, and the previous one added to the committed selection. All functions operate on both layers; for example, selectedItems() will return items from both layers. \sa {Model/View Programming}, QAbstractItemModel, {Chart Example}*//*! Constructs a selection model that operates on the specified item \a model.*/QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model) : QObject(*new QItemSelectionModelPrivate, model){ d_func()->model = model; if (model) { connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int))); connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int))); connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int))); connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int))); connect(model, SIGNAL(layoutAboutToBeChanged()), this, SLOT(_q_layoutAboutToBeChanged())); connect(model, SIGNAL(layoutChanged()), this, SLOT(_q_layoutChanged())); }}/*! Constructs a selection model that operates on the specified item \a model with \a parent.*/QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model, QObject *parent) : QObject(*new QItemSelectionModelPrivate, parent){ d_func()->model = model; if (model) { connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int))); connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int))); connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int))); connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int))); connect(model, SIGNAL(layoutAboutToBeChanged()), this, SLOT(_q_layoutAboutToBeChanged())); connect(model, SIGNAL(layoutChanged()), this, SLOT(_q_layoutChanged())); }}/*! \internal*/QItemSelectionModel::QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model) : QObject(dd, model){ d_func()->model = model; if (model) { connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int))); connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int))); connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int))); connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int))); connect(model, SIGNAL(layoutAboutToBeChanged()), this, SLOT(_q_layoutAboutToBeChanged())); connect(model, SIGNAL(layoutChanged()), this, SLOT(_q_layoutChanged())); }}/*! Destroys the selection model.*/QItemSelectionModel::~QItemSelectionModel(){ Q_D(QItemSelectionModel); if (d->model) { disconnect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int))); disconnect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int))); disconnect(d->model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int))); disconnect(d->model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int))); disconnect(d->model, SIGNAL(layoutAboutToBeChanged()), this, SLOT(_q_layoutAboutToBeChanged())); disconnect(d->model, SIGNAL(layoutChanged()), this, SLOT(_q_layoutChanged())); }}/*! Selects the model item \a index using the specified \a command, and emits selectionChanged(). \sa QItemSelectionModel::SelectionFlags*/void QItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command){ QItemSelection selection(index, index); select(selection, command);}/*! \fn void QItemSelectionModel::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) This signal is emitted whenever the current item changes. The \a previous model item index is replaced by the \a current index as the selection's current item. Note that this signal will not be emitted when the item model is reset. \sa currentIndex() setCurrentIndex() selectionChanged()*//*! \fn void QItemSelectionModel::currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous) This signal is emitted if the \a current item changes and its column is different to the column of the \a previous current item. Note that this signal will not be emitted when the item model is reset. \sa currentChanged() currentRowChanged() currentIndex() setCurrentIndex()*//*! \fn void QItemSelectionModel::currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) This signal is emitted if the \a current item changes and its row is different to the row of the \a previous current item. Note that this signal will not be emitted when the item model is reset. \sa currentChanged() currentColumnChanged() currentIndex() setCurrentIndex()*//*! \fn void QItemSelectionModel::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) This signal is emitted whenever the selection changes. The change in the selection is represented as an item selection of \a deselected items and an item selection of \a selected items. Note the that the current index changes independently from the selection. Also note that this signal will not be emitted when the item model is reset. \sa select() currentChanged()*//*! \enum QItemSelectionModel::SelectionFlag This enum describes the way the selection model will be updated. \value NoUpdate No selection will be made. \value Clear The complete selection will be cleared. \value Select All specified indexes will be selected. \value Deselect All specified indexes will be deselected. \value Toggle All specified indexes will be selected or deselected depending on their current state. \value Current The current selection will be updated. \value Rows All indexes will be expanded to span rows. \value Columns All indexes will be expanded to span columns. \value SelectCurrent A combination of Select and Current, provided for convenience. \value ToggleCurrent A combination of Toggle and Current, provided for convenience. \value ClearAndSelect A combination of Clear and Select, provided for convenience.*//*! Selects the item \a selection using the specified \a command, and emits selectionChanged(). \sa QItemSelectionModel::SelectionFlag*/void QItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command){ Q_D(QItemSelectionModel); if (command == NoUpdate) return; // store old selection QItemSelection sel = selection; QItemSelection old = d->ranges; old.merge(d->currentSelection, d->currentCommand); // expand selection according to SelectionBehavior if (command & Rows || command & Columns) sel = d->expandSelection(sel, command); // clear ranges and currentSelection if (command & Clear) { d->ranges.clear(); d->currentSelection.clear(); } // merge and clear currentSelection if Current was not set (ie. start new currentSelection) if (!(command & Current)) d->finalize(); // update currentSelection if (command & Toggle || command & Select || command & Deselect) { d->currentCommand = command; d->currentSelection = sel; } // generate new selection, compare with old and emit selectionChanged() QItemSelection newSelection = d->ranges; newSelection.merge(d->currentSelection, d->currentCommand); emitSelectionChanged(newSelection, old); return;}/*! Clears the selection model. Emits selectionChanged() and currentChanged().*/void QItemSelectionModel::clear(){ Q_D(QItemSelectionModel); clearSelection(); QModelIndex previous = d->currentIndex; d->currentIndex = QModelIndex(); if (previous.isValid()) { emit currentChanged(d->currentIndex, previous); emit currentRowChanged(d->currentIndex, previous); emit currentColumnChanged(d->currentIndex, previous); }}/*! Clears the selection model. Does not emit any signals.*/void QItemSelectionModel::reset(){ bool block = blockSignals(true); clear(); blockSignals(block);}/*! \since 4.2 Clears the selection in the selection model. Emits selectionChanged().*/void QItemSelectionModel::clearSelection(){ Q_D(QItemSelectionModel); if (d->ranges.count() == 0 && d->currentSelection.count() == 0) return; QItemSelection selection = d->ranges; selection.merge(d->currentSelection, d->currentCommand); d->ranges.clear(); d->currentSelection.clear(); emit selectionChanged(QItemSelection(), selection);}/*! Sets the model item \a index to be the current item, and emits currentChanged(). The current item is used for keyboard navigation and focus indication; it is independent of any selected items, although a selected item can also be the current item. Depending on the specified \a command, the \a index can also become part of the current selection. \sa select()*/void QItemSelectionModel::setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command){ Q_D(QItemSelectionModel); if (index == d->currentIndex) { if (command != NoUpdate) select(index, command); // select item return; } QPersistentModelIndex previous = d->currentIndex; d->currentIndex = index; // set current before emitting selection changed below if (command != NoUpdate) select(d->currentIndex, command); // select item emit currentChanged(d->currentIndex, previous); if (d->currentIndex.row() != previous.row()) emit currentRowChanged(d->currentIndex, previous); if (d->currentIndex.column() != previous.column()) emit currentColumnChanged(d->currentIndex, previous);}/*! Returns the model item index for the current item, or an invalid index if there is no current item.*/QModelIndex QItemSelectionModel::currentIndex() const{ return static_cast<QModelIndex>(d_func()->currentIndex);}/*! Returns true if the given model item \a index is selected.*/bool QItemSelectionModel::isSelected(const QModelIndex &index) const{ Q_D(const QItemSelectionModel); if (d->model != index.model() || !index.isValid()) return false; bool selected = false; // search model ranges QList<QItemSelectionRange>::const_iterator it = d->ranges.begin();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -