📄 qheaderview.cpp
字号:
/*! \fn void QHeaderView::setOffset(int offset) Sets the header's offset to \a offset. \sa offset(), length()*/void QHeaderView::setOffset(int newOffset){ Q_D(QHeaderView); if (d->offset == (uint)newOffset) return; int ndelta = d->offset - newOffset; d->offset = newOffset; if (d->orientation == Qt::Horizontal) d->viewport->scroll(isRightToLeft() ? -ndelta : ndelta, 0); else d->viewport->scroll(0, ndelta); if (d->state == QHeaderViewPrivate::ResizeSection) { QPoint cursorPos = QCursor::pos(); if (d->orientation == Qt::Horizontal) QCursor::setPos(cursorPos.x() + ndelta, cursorPos.y()); else QCursor::setPos(cursorPos.x(), cursorPos.y() + ndelta); d->firstPos += ndelta; d->lastPos += ndelta; }}/*! \since 4.2 Sets the offset to the start of the section at the given \a visualIndex. \sa setOffset(), sectionPosition()*/void QHeaderView::setOffsetToSectionPosition(int visualIndex){ Q_D(QHeaderView); if (visualIndex > -1 && visualIndex < d->sectionCount) { int position = d->headerSectionPosition(d->adjustedVisualIndex(visualIndex)); setOffset(position); }}/*! \since 4.2 Sets the offset to make the last section visible. \sa setOffset(), sectionPosition(), setOffsetToSectionPosition()*/void QHeaderView::setOffsetToLastSection(){ Q_D(const QHeaderView); int size = (d->orientation == Qt::Horizontal ? viewport()->width() : viewport()->height()); int position = length() - size; setOffset(position);}/*! Returns the length along the orientation of the header. \sa sizeHint(), setResizeMode(), offset()*/int QHeaderView::length() const{ Q_D(const QHeaderView); //Q_ASSERT(d->headerLength() == d->length); return d->length;}/*! Returns a suitable size hint for this header. \sa sectionSizeHint()*/QSize QHeaderView::sizeHint() const{ Q_D(const QHeaderView); if (count() < 1) return QSize(0, 0); if (d->cachedSizeHint.isValid()) return d->cachedSizeHint; int width = 0; int height = 0; // get size hint for the first n sections int c = qMin(count(), 100); for (int i = 0; i < c; ++i) { if (isSectionHidden(i)) continue; QSize hint = sectionSizeFromContents(i); width = qMax(hint.width(), width); height = qMax(hint.height(), height); } // get size hint for the last n sections c = qMax(count() - 100, c); for (int j = count() - 1; j >= c; --j) { if (isSectionHidden(j)) continue; QSize hint = sectionSizeFromContents(j); width = qMax(hint.width(), width); height = qMax(hint.height(), height); } d->cachedSizeHint = QSize(width, height); return d->cachedSizeHint;}/*! Returns a suitable size hint for the section specified by \a logicalIndex. \sa sizeHint(), defaultSectionSize(), minimumSectionSize(), Qt::SizeHintRole*/int QHeaderView::sectionSizeHint(int logicalIndex) const{ Q_D(const QHeaderView); if (isSectionHidden(logicalIndex)) return 0; if (logicalIndex < 0 || logicalIndex >= count()) return -1; QSize size; QVariant value = d->model->headerData(logicalIndex, d->orientation, Qt::SizeHintRole); if (value.isValid()) size = qvariant_cast<QSize>(value); else size = sectionSizeFromContents(logicalIndex); int hint = d->orientation == Qt::Horizontal ? size.width() : size.height(); return qMax(minimumSectionSize(), hint);}/*! Returns the visual index of the section that covers the given \a position in the viewport. \sa logicalIndexAt()*/int QHeaderView::visualIndexAt(int position) const{ Q_D(const QHeaderView); uint vposition = position; d->executePostedLayout(); const int count = d->sectionCount; if (count < 1) return -1; if (d->reverse()) vposition = d->viewport->width() - vposition; vposition += d->offset; if (vposition > d->length) return -1; int visual = d->headerVisualIndexAt(vposition); if (visual < 0) return -1; while (d->isVisualIndexHidden(visual)){ ++visual; if (visual >= count) return -1; } return visual;}/*! Returns the section that covers the given \a position in the viewport. \sa visualIndexAt(), isSectionHidden()*/int QHeaderView::logicalIndexAt(int position) const{ const int visual = visualIndexAt(position); if (visual > -1) return logicalIndex(visual); return -1;}/*! Returns the width (or height for vertical headers) of the given \a logicalIndex. \sa length(), setResizeMode(), defaultSectionSize()*/int QHeaderView::sectionSize(int logicalIndex) const{ Q_D(const QHeaderView); if (isSectionHidden(logicalIndex)) return 0; if (logicalIndex < 0 || logicalIndex >= count()) return 0; int visual = visualIndex(logicalIndex); if (visual == -1) return 0; return d->headerSectionSize(visual);}/*! Returns the section position of the given \a logicalIndex, or -1 if the section is hidden. \sa sectionViewportPosition()*/int QHeaderView::sectionPosition(int logicalIndex) const{ Q_D(const QHeaderView); int visual = visualIndex(logicalIndex); // in some cases users may change the selections // before we have a chance to do the layout if (visual == -1) return -1; return d->headerSectionPosition(visual);}/*! Returns the section viewport position of the given \a logicalIndex. If the section is hidden, this function returns an undefined value. \sa sectionPosition(), isSectionHidden()*/int QHeaderView::sectionViewportPosition(int logicalIndex) const{ Q_D(const QHeaderView); if (logicalIndex >= count()) return -1; int position = sectionPosition(logicalIndex); if (position < 0) return position; // the section was hidden int offsetPosition = position - d->offset; if (d->reverse()) return d->viewport->width() - (offsetPosition + sectionSize(logicalIndex)); return offsetPosition;}/*! \fn int QHeaderView::logicalIndexAt(int x, int y) const Returns the logical index of the section at the given coordinate. If the header is horizontal \a x will be used, otherwise \a y will be used to find the logical index.*//*! \fn int QHeaderView::logicalIndexAt(const QPoint &pos) const Returns the logical index of the section at the position given in \a pos. If the header is horizontal the x-coordinate will be used to find the logical index; otherwise the y-coordinate will be used. \sa sectionPosition()*//*! Moves the section at visual index \a from to occupy visual index \a to. \sa sectionsMoved()*/void QHeaderView::moveSection(int from, int to){ Q_D(QHeaderView); d->executePostedLayout(); if (from < 0 || from >= d->sectionCount || to < 0 || to >= d->sectionCount) return; if (from == to) { int logical = logicalIndex(from); Q_ASSERT(logical != -1); updateSection(logical); return; } //int oldHeaderLength = length(); // ### for debugging; remove later d->initializeIndexMapping(); QBitArray sectionHidden = d->sectionHidden; int *visualIndices = d->visualIndices.data(); int *logicalIndices = d->logicalIndices.data(); int logical = logicalIndices[from]; int visual = from; int affected_count = qAbs(to - from) + 1; QVarLengthArray<int> sizes(affected_count); QVarLengthArray<ResizeMode> modes(affected_count); // move sections and indices if (to > from) { sizes[to - from] = d->headerSectionSize(from); modes[to - from] = d->headerSectionResizeMode(from); while (visual < to) { sizes[visual - from] = d->headerSectionSize(visual + 1); modes[visual - from] = d->headerSectionResizeMode(visual + 1); if (!sectionHidden.isEmpty()) sectionHidden.setBit(visual, sectionHidden.testBit(visual + 1)); visualIndices[logicalIndices[visual + 1]] = visual; logicalIndices[visual] = logicalIndices[visual + 1]; ++visual; } } else { sizes[0] = d->headerSectionSize(from); modes[0] = d->headerSectionResizeMode(from); while (visual > to) { sizes[visual - to] = d->headerSectionSize(visual - 1); modes[visual - to] = d->headerSectionResizeMode(visual - 1); if (!sectionHidden.isEmpty()) sectionHidden.setBit(visual, sectionHidden.testBit(visual - 1)); visualIndices[logicalIndices[visual - 1]] = visual; logicalIndices[visual] = logicalIndices[visual - 1]; --visual; } } if (!sectionHidden.isEmpty()) { sectionHidden.setBit(to, d->sectionHidden.testBit(from)); d->sectionHidden = sectionHidden; } visualIndices[logical] = to; logicalIndices[to] = logical; //Q_ASSERT(oldHeaderLength == length()); // move sizes // ### check for spans of section sizes here if (to > from) { for (visual = from; visual <= to; ++visual) { int size = sizes[visual - from]; ResizeMode mode = modes[visual - from]; d->createSectionSpan(visual, visual, size, mode); } } else { for (visual = to; visual <= from; ++visual) { int size = sizes[visual - to]; ResizeMode mode = modes[visual - to]; d->createSectionSpan(visual, visual, size, mode); } } //Q_ASSERT(d->headerLength() == length()); //Q_ASSERT(oldHeaderLength == length()); //Q_ASSERT(d->logicalIndices.count() == d->sectionCount); if (d->hasAutoResizeSections()) resizeSections(); d->viewport->update(); emit sectionMoved(logical, from, to);}/*! \since 4.2 Swaps the section at visual index \a first with the section at visual index \a second. \sa moveSection()*/void QHeaderView::swapSections(int first, int second){ Q_D(QHeaderView); if (first == second) return; d->executePostedLayout(); if (first < 0 || first >= d->sectionCount || second < 0 || second >= d->sectionCount) return; int firstSize = d->headerSectionSize(first); ResizeMode firstMode = d->headerSectionResizeMode(first); int firstLogical = d->logicalIndex(first); int secondSize = d->headerSectionSize(second); ResizeMode secondMode = d->headerSectionResizeMode(second); int secondLogical = d->logicalIndex(second); d->createSectionSpan(second, second, firstSize, firstMode); d->createSectionSpan(first, first, secondSize, secondMode); d->initializeIndexMapping(); d->visualIndices[firstLogical] = second; d->logicalIndices[second] = firstLogical; d->visualIndices[secondLogical] = first; d->logicalIndices[first] = secondLogical; if (!d->sectionHidden.isEmpty()) { bool firstHidden = d->sectionHidden.testBit(first); bool secondHidden = d->sectionHidden.testBit(second); d->sectionHidden.setBit(first, secondHidden); d->sectionHidden.setBit(second, firstHidden); } d->viewport->update(); emit sectionMoved(firstLogical, first, second); emit sectionMoved(secondLogical, second, first);}/*! \fn void QHeaderView::resizeSection(int logicalIndex, int size) Resizes the section specified by \a logicalIndex to the \a size measured in pixels. \sa sectionResized(), resizeMode(), sectionSize()*/void QHeaderView::resizeSection(int logical, int size){ Q_D(QHeaderView); if (logical < 0 || logical >= count()) return; if (isSectionHidden(logical)) { d->hiddenSectionSize.insert(logical, size); return; } int oldSize = sectionSize(logical);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -