📄 qheaderview.cpp
字号:
Q_D(const QHeaderView); return d->sortIndicatorOrder;}/*! \property QHeaderView::stretchLastSection \brief whether the last visible section in the header takes up all the available space The default value is false. \bold{Note:} The horizontal headers provided by QTreeView are configured with this property set to true, ensuring that the view does not waste any of the space assigned to it for its header. \bold{Also note:} If the value is set to true, this property will override the resize mode set on the last section in the header. \sa setResizeMode()*/bool QHeaderView::stretchLastSection() const{ Q_D(const QHeaderView); return d->stretchLastSection;}void QHeaderView::setStretchLastSection(bool stretch){ Q_D(QHeaderView); d->stretchLastSection = stretch; if (d->state != QHeaderViewPrivate::NoState) return; if (stretch) resizeSections(); else if (count()) resizeSection(count() - 1, d->defaultSectionSize);}/*! \since 4.2 \property QHeaderView::cascadingSectionResizes \brief whether interactive resizing will be cascaded to the following sections once the section being resized by the user has reached its minimum size This property only affects sections that have \l Interactive as the resize mode. The default value is false. \sa setResizeMode()*/bool QHeaderView::cascadingSectionResizes() const{ Q_D(const QHeaderView); return d->cascadingResizing;}void QHeaderView::setCascadingSectionResizes(bool enable){ Q_D(QHeaderView); d->cascadingResizing = enable;}/*! \property QHeaderView::defaultSectionSize \brief the default size of the header sections before resizing. This property only affects sections that have \l Interactive or \l Fixed as the resize mode. \sa setResizeMode() minimumSectionSize*/int QHeaderView::defaultSectionSize() const{ Q_D(const QHeaderView); return d->defaultSectionSize;}void QHeaderView::setDefaultSectionSize(int size){ Q_D(QHeaderView); d->defaultSectionSize = size; d->forceInitializing = true;}/*! \since 4.2 \property QHeaderView::minimumSectionSize \brief the minimum size of the header sections. The minimum section size is the smallest section size allowed. If the minimum section size is set to -1, QHeaderView will use the maximum of the \l{QApplication::globalStrut()}{global strut} or the \l{fontMetrics()}{font metrics} size. This property is honored by all \l{ResizeMode}{resize modes}. \sa setResizeMode() defaultSectionSize*/int QHeaderView::minimumSectionSize() const{ Q_D(const QHeaderView); if (d->minimumSectionSize == -1) { QSize strut = QApplication::globalStrut(); int margin = style()->pixelMetric(QStyle::PM_HeaderMargin, 0, this); if (d->orientation == Qt::Horizontal) return qMax(strut.width(), (fontMetrics().maxWidth() + margin)); return qMax(strut.height(), (fontMetrics().lineSpacing() + margin)); } return d->minimumSectionSize;}void QHeaderView::setMinimumSectionSize(int size){ Q_D(QHeaderView); d->minimumSectionSize = size;}/*! \since 4.1 \property QHeaderView::defaultAlignment \brief the default alignment of the text in each header section*/Qt::Alignment QHeaderView::defaultAlignment() const{ Q_D(const QHeaderView); return d->defaultAlignment;}void QHeaderView::setDefaultAlignment(Qt::Alignment alignment){ Q_D(QHeaderView); if (d->defaultAlignment == alignment) return; d->defaultAlignment = alignment; d->viewport->update();}/*! \internal*/void QHeaderView::doItemsLayout(){ initializeSections(); QAbstractItemView::doItemsLayout();}/*! Returns true if sections in the header has been moved; otherwise returns false; \sa moveSection()*/bool QHeaderView::sectionsMoved() const{ Q_D(const QHeaderView); return !d->visualIndices.isEmpty();}/*! \since 4.1 Returns true if sections in the header has been hidden; otherwise returns false; \sa setSectionHidden()*/bool QHeaderView::sectionsHidden() const{ Q_D(const QHeaderView); return !d->hiddenSectionSize.isEmpty();}#ifndef QT_NO_DATASTREAM/*! \since 4.3 Saves the current state of this header view. To restore the saved state, pass the return value to restoreState(). \sa restoreState()*/QByteArray QHeaderView::saveState() const{ Q_D(const QHeaderView); QByteArray data; QDataStream stream(&data, QIODevice::WriteOnly); stream << QHeaderViewPrivate::VersionMarker; stream << 0; // current version is 0 d->write(stream); return data;}/*! \since 4.3 Restores the \a state of this header view. This function returns \c true if the state was restored, otherwise returns false. \sa saveState()*/bool QHeaderView::restoreState(const QByteArray &state){ Q_D(QHeaderView); if (state.isEmpty()) return false; QByteArray data = state; QDataStream stream(&data, QIODevice::ReadOnly); int marker; int ver; stream >> marker; stream >> ver; if (stream.status() != QDataStream::Ok || marker != QHeaderViewPrivate::VersionMarker || ver != 0) // current version is 0 return false; return d->read(stream);}#endif/*! \reimp*/void QHeaderView::reset(){ QAbstractItemView::reset(); // it would be correct to call clear, but some apps rely // on the header keeping the sections, even after calling reset //d->clear(); initializeSections();}/*! Updates the changed header sections with the given \a orientation, from \a logicalFirst to \a logicalLast inclusive.*/void QHeaderView::headerDataChanged(Qt::Orientation orientation, int logicalFirst, int logicalLast){ Q_D(QHeaderView); if (d->orientation != orientation) return; if (logicalFirst < 0 || logicalLast < 0 || logicalFirst >= count() || logicalLast >= count()) return; d->invalidateCachedSizeHint(); if (orientation == Qt::Horizontal) { int left = sectionViewportPosition(logicalFirst); int right = sectionViewportPosition(logicalLast); right += sectionSize(logicalLast); d->viewport->update(left, 0, right - left, d->viewport->height()); } else { int top = sectionViewportPosition(logicalFirst); int bottom = sectionViewportPosition(logicalLast); bottom += sectionSize(logicalLast); d->viewport->update(0, top, d->viewport->width(), bottom - top); }}/*! \internal \since 4.2 Updates the section specified by the given \a logicalIndex.*/void QHeaderView::updateSection(int logicalIndex){ Q_D(QHeaderView); if (d->orientation == Qt::Horizontal) d->viewport->update(QRect(sectionViewportPosition(logicalIndex), 0, sectionSize(logicalIndex), d->viewport->height())); else d->viewport->update(QRect(0, sectionViewportPosition(logicalIndex), d->viewport->width(), sectionSize(logicalIndex)));}/*! Resizes the sections according to their size hints. You should not normally need to call this function.*/void QHeaderView::resizeSections(){ Q_D(QHeaderView); if (d->hasAutoResizeSections()) d->resizeSections(Interactive, false); // no global resize mode}/*! This slot is called when sections are inserted into the \a parent, \a logicalFirst and \a logicalLast indexes signify where the new sections are inserted. \a logicalFirst and \a logicalLast will be the same if just one section is inserted.*/void QHeaderView::sectionsInserted(const QModelIndex &parent, int logicalFirst, int logicalLast){ Q_D(QHeaderView); if (parent != d->root) return; // we only handle changes in the top level int oldCount = d->sectionCount; d->invalidateCachedSizeHint(); // add the new sections int insertAt = 0; for (int spanStart = 0; insertAt < d->sectionSpans.count() && spanStart < logicalFirst; ++insertAt) spanStart += d->sectionSpans.at(insertAt).count; int insertCount = logicalLast - logicalFirst + 1; d->sectionCount += insertCount; if (d->sectionSpans.isEmpty() || insertAt >= d->sectionSpans.count()) { int insertLength = d->defaultSectionSize * insertCount; d->length += insertLength; QHeaderViewPrivate::SectionSpan span(insertLength, insertCount, d->globalResizeMode); d->sectionSpans.append(span); } else if ((d->sectionSpans.at(insertAt).sectionSize() == d->defaultSectionSize) && d->sectionSpans.at(insertAt).resizeMode == d->globalResizeMode) { // add the new sections to an existing span int insertLength = d->sectionSpans.at(insertAt).sectionSize() * insertCount; d->length += insertLength; d->sectionSpans[insertAt].size += insertLength; d->sectionSpans[insertAt].count += insertCount; } else { // separate them out into their own span int insertLength = d->defaultSectionSize * insertCount; d->length += insertLength; QHeaderViewPrivate::SectionSpan span(insertLength, insertCount, d->globalResizeMode); d->sectionSpans.insert(insertAt, span); } // update resize mode section counts if (d->globalResizeMode == Stretch) d->stretchSections = d->sectionCount; else if (d->globalResizeMode == ResizeToContents) d->contentsSections = d->sectionCount; // insert new sections in sectionsHidden if (!d->sectionHidden.isEmpty()) { QBitArray sectionHidden(d->sectionHidden); sectionHidden.resize(sectionHidden.count() + insertCount); //sectionHidden.fill(false, logicalFirst, logicalLast + 1); for (int i = logicalFirst; i <= logicalLast; ++i) sectionHidden.setBit(i, false); for (int j = logicalLast + 1; j < sectionHidden.count(); ++j) sectionHidden.setBit(j, d->sectionHidden.testBit(j - insertCount)); d->sectionHidden = sectionHidden; } // clear selection cache d->sectionSelected.clear(); // update mapping if (!d->visualIndices.isEmpty() && !d->logicalIndices.isEmpty()) { Q_ASSERT(d->visualIndices.count() == d->logicalIndices.count()); int mappingCount = d->visualIndices.count(); for (int i = 0; i < mappingCount; ++i) { if (d->visualIndices.at(i) >= logicalFirst) d->visualIndices[i] += insertCount; if (d->logicalIndices.at(i) >= logicalFirst) d->logicalIndices[i] += insertCount; } for (int j = logicalFirst; j <= logicalLast; ++j) { d->visualIndices.insert(j, j); d->logicalIndices.insert(j, j); } } resizeSections(); emit sectionCountChanged(oldCount, count()); // if the new sections were not updated by resizing, we need to update now if (!d->hasAutoResizeSections()) d->viewport->update();}/*! This slot is called when sections are removed from the \a parent, \a logicalFirst and \a logicalLast signify where the sections are removed from. (\a logicalFirst and \a logicalLast will be the same if just one section is removed.)*/void QHeaderView::sectionsAboutToBeRemoved(const QModelIndex &parent, int logicalFirst, int logicalLast){ Q_UNUSED(parent); Q_UNUSED(logicalFirst); Q_UNUSED(logicalLast);}void QHeaderViewPrivate::_q_sectionsRemoved(const QModelIndex &parent, int logicalFirst, int logicalLast){ Q_Q(QHeaderView); if (parent != root) return; // we only handle changes in the top level if (qMin(logicalFirst, logicalLast) < 0 || qMax(logicalLast, logicalFirst) >= sectionCount) return; int oldCount = q->count(); int changeCount = logicalLast - logicalFirst + 1; // remove sections in sectionsHidden if (!sectionHidden.isEmpty()) { const int newsize = qMin(sectionCount - changeCount, sectionHidden.size()); QBitArray newSectionHidden(newsize); for(int j = 0, k = 0; j < sectionHidden.size(); ++j) { const int logical = q->logicalIndex(j); if (logical < logicalFirst || logical > logicalLast) { newSectionHidden[k++] = sectionHidden[j];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -