📄 qtreeview.cpp
字号:
}/*! \property QTreeView::uniformRowHeights \brief whether all items in the treeview have the same height This property should only be set to true if it is guaranteed that all items in the view has the same height. This enables the view to do some optimizations. The height is obtained from the first item in the view. It is updated when the data changes on that item.*/bool QTreeView::uniformRowHeights() const{ Q_D(const QTreeView); return d->uniformRowHeights;}void QTreeView::setUniformRowHeights(bool uniform){ Q_D(QTreeView); d->uniformRowHeights = uniform;}/*! \property QTreeView::itemsExpandable \brief whether the items are expandable by the user. This property holds whether the user can expand and collapse items interactively.*/bool QTreeView::itemsExpandable() const{ Q_D(const QTreeView); return d->itemsExpandable;}void QTreeView::setItemsExpandable(bool enable){ Q_D(QTreeView); d->itemsExpandable = enable;}/*! Returns the horizontal position of the \a column in the viewport.*/int QTreeView::columnViewportPosition(int column) const{ Q_D(const QTreeView); return d->header->sectionViewportPosition(column);}/*! Returns the width of the \a column. \sa resizeColumnToContents(), setColumnWidth()*/int QTreeView::columnWidth(int column) const{ Q_D(const QTreeView); return d->header->sectionSize(column);}/*! \since 4.2 Sets the width of the given \a column to the \a width specified. \sa columnWidth(), resizeColumnToContents()*/void QTreeView::setColumnWidth(int column, int width){ Q_D(QTreeView); d->header->resizeSection(column, width);}/*! Returns the column in the tree view whose header covers the \a x coordinate given.*/int QTreeView::columnAt(int x) const{ Q_D(const QTreeView); return d->header->logicalIndexAt(x);}/*! Returns true if the \a column is hidden; otherwise returns false. \sa hideColumn(), isRowHidden()*/bool QTreeView::isColumnHidden(int column) const{ Q_D(const QTreeView); return d->header->isSectionHidden(column);}/*! If \a hide is true the \a column is hidden, otherwise the \a column is shown. \sa hideColumn(), setRowHidden()*/void QTreeView::setColumnHidden(int column, bool hide){ Q_D(QTreeView); if (column < 0 || column >= d->header->count()) return; d->header->setSectionHidden(column, hide);}/*! Returns true if the item in the given \a row of the \a parent is hidden; otherwise returns false. \sa setRowHidden(), isColumnHidden()*/bool QTreeView::isRowHidden(int row, const QModelIndex &parent) const{ Q_D(const QTreeView); if (d->hiddenIndexes.isEmpty() || !d->model) return false; QModelIndex index = d->model->index(row, 0, parent); for (int i = 0; i < d->hiddenIndexes.count(); ++i) if (d->hiddenIndexes.at(i) == index) return true; return false;}/*! If \a hide is true the \a row with the given \a parent is hidden, otherwise the \a row is shown. \sa isRowHidden(), setColumnHidden()*/void QTreeView::setRowHidden(int row, const QModelIndex &parent, bool hide){ Q_D(QTreeView); if (!d->model) return; QModelIndex index = d->model->index(row, 0, parent); if (!index.isValid()) return; if (hide) { QPersistentModelIndex persistent(index); if (!d->hiddenIndexes.contains(persistent)) d->hiddenIndexes.append(persistent); } else { QPersistentModelIndex persistent(index); int i = d->hiddenIndexes.indexOf(persistent); if (i >= 0) d->hiddenIndexes.remove(i); } if (hide && isVisible()) { int p = d->viewIndex(parent); if (p >= 0) { const int first = p + 1; const int last = first + d->viewItems.at(p).total - 1; for (int i = first; i <= last; ) { const int count = d->viewItems.at(i).total + 1; if (d->viewItems.at(i).index == index) { // remove child and its children d->viewItems.remove(i, count); // update children count of ancestors d->updateChildCount(p, -count); break; } else { i += count; } } d->executePostedLayout(); updateGeometries(); d->viewport->update(); } else { d->doDelayedItemsLayout(); } } else { d->doDelayedItemsLayout(); }}/*! \since 4.3 Returns true if the item in first column in the given \a row of the \a parent is spanning all the columns; otherwise returns false. \sa setFirstColumnSpanned()*/bool QTreeView::isFirstColumnSpanned(int row, const QModelIndex &parent) const{ Q_D(const QTreeView); if (d->spanningIndexes.isEmpty() || !d->model) return false; QModelIndex index = d->model->index(row, 0, parent); for (int i = 0; i < d->spanningIndexes.count(); ++i) if (d->spanningIndexes.at(i) == index) return true; return false;}/*! \since 4.3 If \a span is true the item in the first column in the \a row with the given \a parent is set to span all columns, otherwise all items on the \a row are shown. \sa isFirstColumnSpanned()*/void QTreeView::setFirstColumnSpanned(int row, const QModelIndex &parent, bool span){ Q_D(QTreeView); if (!d->model) return; QModelIndex index = d->model->index(row, 0, parent); if (!index.isValid()) return; if (span) { QPersistentModelIndex persistent(index); if (!d->spanningIndexes.contains(persistent)) d->spanningIndexes.append(persistent); } else { QPersistentModelIndex persistent(index); int i = d->spanningIndexes.indexOf(persistent); if (i >= 0) d->spanningIndexes.remove(i); } d->executePostedLayout(); int i = d->viewIndex(index); if (i >= 0) d->viewItems[i].spanning = span; d->viewport->update();}/*! \reimp*/void QTreeView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight){ Q_D(QTreeView); // if we are going to do a complete relayout anyway, there is no need to update if (d->delayedLayout.isActive()) return; // refresh the height cache here; we don't really lose anything by getting the size hint, // since QAbstractItemView::dataChanged() will get the visualRect for the items anyway QModelIndex top = (topLeft.column() == 0) ? topLeft : d->model->sibling(topLeft.row(), 0, topLeft); int topViewIndex = d->viewIndex(top); if (topViewIndex == 0) d->defaultItemHeight = indexRowSizeHint(top); bool sizeChanged = false; if (topViewIndex != -1) { if (topLeft == bottomRight) { int oldHeight = d->itemHeight(topViewIndex); d->invalidateHeightCache(topViewIndex); sizeChanged = (oldHeight != d->itemHeight(topViewIndex)); } else { QModelIndex bottom = (bottomRight.column() == 0) ? bottomRight : d->model->sibling(bottomRight.row(), 0, bottomRight); int bottomViewIndex = d->viewIndex(bottom); for (int i = topViewIndex; i <= bottomViewIndex; ++i) { int oldHeight = d->itemHeight(i); d->invalidateHeightCache(i); sizeChanged |= (oldHeight != d->itemHeight(i)); } } } if (sizeChanged) { d->updateScrollBars(); d->viewport->update(); } QAbstractItemView::dataChanged(topLeft, bottomRight);}/*! Hides the \a column given. \note This function should only be called after the model has been initialized, as the view needs to know the number of columns in order to hide \a column. \sa showColumn(), setColumnHidden()*/void QTreeView::hideColumn(int column){ Q_D(QTreeView); d->header->hideSection(column);}/*! Shows the given \a column in the tree view. \sa hideColumn(), setColumnHidden()*/void QTreeView::showColumn(int column){ Q_D(QTreeView); d->header->showSection(column);}/*! \fn void QTreeView::expand(const QModelIndex &index) Expands the model item specified by the \a index. \sa expanded()*/void QTreeView::expand(const QModelIndex &index){ Q_D(QTreeView); if (!d->isIndexValid(index)) return; d->executePostedLayout(); int i = d->viewIndex(index); if (i != -1) { // is visible d->expand(i, true); if (!d->isAnimating()) { updateGeometries(); d->viewport->update(); } } else if (!d->expandedIndexes.contains(index)) { d->expandedIndexes.append(index); emit expanded(index); }}/*! \fn void QTreeView::collapse(const QModelIndex &index) Collapses the model item specified by the \a index. \sa collapsed()*/void QTreeView::collapse(const QModelIndex &index){ Q_D(QTreeView); if (!d->isIndexValid(index)) return; d->executePostedLayout(); int i = d->viewIndex(index); if (i != -1) { // is visible d->collapse(i, true); if (!d->isAnimating()) { updateGeometries(); viewport()->update(); } } else { int i = d->expandedIndexes.indexOf(index); if (i != -1) { d->expandedIndexes.remove(i); emit collapsed(index); } }}/*! \fn bool QTreeView::isExpanded(const QModelIndex &index) const Returns true if the model item \a index is expanded; otherwise returns false. \sa expand(), expanded(), setExpanded()*/bool QTreeView::isExpanded(const QModelIndex &index) const{ Q_D(const QTreeView); d->executePostedLayout(); int i = d->viewIndex(index); if (i != -1) // is visible return d->viewItems.at(i).expanded; return d->expandedIndexes.contains(index);}/*! Sets the item referred to by \a index to either collapse or expanded, depending on the value of \a expanded. \sa expanded(), expand(), isExpanded()*/void QTreeView::setExpanded(const QModelIndex &index, bool expanded){ if (expanded) this->expand(index); else this->collapse(index);}/*! \since Qt 4.2 \property QTreeView::sortingEnabled
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -