📄 qheaderview.cpp
字号:
while (sections.at(visual).hidden && visual < count) ++visual; if (visual >= count || (visual == end && sections.at(end + 1).position < position)) 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{ return logicalIndex(visualIndexAt(position));}/*! 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 (logicalIndex < 0 || logicalIndex >= count()) return 0; if (isSectionHidden(logicalIndex)) return 0; int visual = visualIndex(logicalIndex); if (visual == -1) return 0; return d->sections.at(visual + 1).position - d->sections.at(visual).position;}/*! Returns the section position of the given \a logicalIndex. \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->sections.at(visual).position;}/*! Returns the section viewport position of the given \a logicalIndex. If the section is hidden the function returns -1. \sa sectionPosition()*/int QHeaderView::sectionViewportPosition(int logicalIndex) const{ Q_D(const QHeaderView); if (logicalIndex < 0 || 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(); Q_ASSERT(from >= 0 && from < d->sections.count() - 1); Q_ASSERT(to >= 0 && to < d->sections.count() - 1); if (from == to) { int logical = logicalIndex(from); Q_ASSERT(logical != -1); updateSection(logical); return; } // if we haven't moved anything previously, initialize the indices array int count = d->sections.count(); if (d->visualIndices.count() != count || d->logicalIndices.count() != count) { d->visualIndices.resize(count); d->logicalIndices.resize(count); for (int s = 0; s < count; ++s) { d->visualIndices[s] = s; d->logicalIndices[s] = s; } } QHeaderViewPrivate::HeaderSection *sections = d->sections.data(); int *visualIndices = d->visualIndices.data(); int *logicalIndices = d->logicalIndices.data(); int logical = logicalIndices[from]; int visual = from; bool hidden = sections[from].hidden; QHeaderView::ResizeMode mode = sections[from].mode; QVarLengthArray<int> sizes(qAbs(to - from) + 1); // move sections and indices if (to > from) { sizes[to - from] = sections[from + 1].position - sections[from].position; while (visual < to) { sizes[visual - from] = sections[visual + 2].position - sections[visual + 1].position; sections[visual].hidden = sections[visual + 1].hidden; sections[visual].mode = sections[visual + 1].mode; visualIndices[logicalIndices[visual + 1]] = visual; logicalIndices[visual] = logicalIndices[visual + 1]; ++visual; } } else { sizes[0] = sections[from + 1].position - sections[from].position; while (visual > to) { sizes[visual - to] = sections[visual].position - sections[visual - 1].position; sections[visual].hidden = sections[visual - 1].hidden; sections[visual].mode = sections[visual - 1].mode; visualIndices[logicalIndices[visual - 1]] = visual; logicalIndices[visual] = logicalIndices[visual - 1]; --visual; } } sections[to].hidden = hidden; sections[to].mode = mode; visualIndices[logical] = to; logicalIndices[to] = logical; // move "pixel" positions if (to > from) { for (visual = from; visual <= to; ++visual) sections[visual + 1].position = sections[visual].position + sizes[visual - from]; } else { for (visual = to; visual <= from; ++visual) sections[visual + 1].position = sections[visual].position + sizes[visual - to]; } d->viewport->update(); emit sectionMoved(logical, from, to);}/*! Resizes the given \a logicalIndex to the given \a size. \sa sectionResized()*/void QHeaderView::resizeSection(int logicalIndex, int size){ Q_D(QHeaderView); if (logicalIndex < 0 || logicalIndex >= count()) return; if (isSectionHidden(logicalIndex)) return; int oldSize = sectionSize(logicalIndex); if (oldSize == size) return; d->executePostedLayout(); d->invalidateCachedSizeHint(); int diff = size - oldSize; int visual = visualIndex(logicalIndex); Q_ASSERT(visual != -1); QHeaderViewPrivate::HeaderSection *sections = d->sections.data() + visual + 1; int num = d->sections.size() - visual - 1; while (num >= 4) { sections[0].position += diff; sections[1].position += diff; sections[2].position += diff; sections[3].position += diff; sections += 4; num -= 4; } if (num > 0) { sections[0].position += diff; if (num > 1) { sections[1].position += diff; if (num > 2) { sections[2].position += diff; } } } int w = d->viewport->width(); int h = d->viewport->height(); int pos = sectionViewportPosition(logicalIndex); QRect r; if (orientation() == Qt::Horizontal) if (isRightToLeft()) r.setRect(0, 0, pos + size, h); else r.setRect(pos, 0, w - pos, h); else r.setRect(0, pos, w, h - pos); if (d->hasAutoResizeSections()) { resizeSections(); r = d->viewport->rect(); } d->viewport->update(r.normalized()); emit sectionResized(logicalIndex, oldSize, size);}/*! Resizes the sections according to the given \a mode, ignoring the current resize mode. \sa resizeMode(), sectionResized()*/void QHeaderView::resizeSections(QHeaderView::ResizeMode mode){ Q_D(QHeaderView); d->resizeSections(mode, true);}/*! \fn void QHeaderView::hideSection(int logicalIndex) Hides the section specified by \a logicalIndex. \sa showSection(), isSectionHidden(), hiddenSectionCount()*//*! \fn void QHeaderView::showSection(int logicalIndex) Shows the section specified by \a logicalIndex. \sa hideSection(), isSectionHidden()*//*! Returns true if the section specified by \a logicalIndex is explicitly hidden from the user; otherwise returns false. \sa setSectionHidden(), hiddenSectionCount()*/bool QHeaderView::isSectionHidden(int logicalIndex) const{ Q_D(const QHeaderView); if (logicalIndex < 0 || logicalIndex >= count()) return false; d->executePostedLayout(); if (logicalIndex >= d->sections.count() - 1) return false; int visual = visualIndex(logicalIndex); Q_ASSERT(visual != -1); return d->sections.at(visual).hidden;}/*! \since 4.1 Returns the number of sections in the header that has been hidden. \sa setSectionHidden(), isSectionHidden()*/int QHeaderView::hiddenSectionCount() const{ Q_D(const QHeaderView); return d->hiddenSectionSize.count();}/*! If \a hide is true the section specified by \a logicalIndex is hidden, otherwise the section is shown. \sa isSectionHidden(), hiddenSectionCount()*/void QHeaderView::setSectionHidden(int logicalIndex, bool hide){ Q_D(QHeaderView); if (logicalIndex < 0 || logicalIndex >= count()) return; d->executePostedLayout(); int visual = visualIndex(logicalIndex); Q_ASSERT(visual != -1); if (hide && d->sections.at(visual).hidden) return; if (hide) { int size = sectionSize(logicalIndex); resizeSection(logicalIndex, 0); d->hiddenSectionSize.insert(logicalIndex, size); d->sections[visual].hidden = true; // A bit of a hack because resizeSection has to called before hiding FIXME if (/*isVisible() && */d->hasAutoResizeSections()) resizeSections(); // changes because of the new/old hiddne } else if (d->sections.at(visual).hidden) { int size = d->hiddenSectionSize.value(logicalIndex, d->defaultSectionSize); d->hiddenSectionSize.remove(logicalIndex); d->sections[visual].hidden = false; resizeSection(logicalIndex, size); }}/*! Returns the number of sections in the header. \sa sectionCountChanged(), length()*/int QHeaderView::count() const{ Q_D(const QHeaderView); int c = d->sections.count(); return c > 0 ? c - 1 : 0;}/*! Returns the visual index position of the section specified by the given \a logicalIndex, or -1 otherwise. Hidden sections still have valid visual indexes. \sa logicalIndex()*/int QHeaderView::visualIndex(int logicalIndex) const{ Q_D(const QHeaderView); if (logicalIndex < 0) return -1; d->executePostedLayout(); if (d->visualIndices.isEmpty()) { // nothing has been moved, so we have no mapping if (logicalIndex < d->sections.count() - 1) return logicalIndex; } else if (logicalIndex < d->visualIndices.count() - 1) { int visual = d->visualIndices.at(logicalIndex); Q_ASSERT(visual < d->sections.count() - 1); return visual; } return -1;}/*! Returns the logicalIndex for the section at the given \a visualIndex position, or -1 otherwise. \sa visualIndex(), sectionPosition()*/int QHeaderView::logicalIndex(int visualIndex) const{ Q_D(const QHeaderView); if (visualIndex < 0 || visualIndex >= d->sections.count()) return -1; return d->logicalIndex(visualIndex);}/*! If \a movable is true, the header may be moved by the user; otherwise it is fixed in place. \sa isMovable(), sectionMoved()*/void QHeaderView::setMovable(bool movable){ Q_D(QHeaderView); d->movableSections = movable;}/*! Returns true if the header can be moved by the user; otherwise returns false. \sa setMovable()*/bool QHeaderView::isMovable() const{ Q_D(const QHeaderView); return d->movableSections;}/*! If \a clickable is true, the header will respond to single clicks. \sa isClickable(), sectionClicked(), sectionPressed(), setSortIndicatorShown()*/void QHeaderView::setClickable(bool clickable){ Q_D(QHeaderView); d->clickableSections = clickable;}/*! Returns true if the header is clickable; otherwise returns false. A clickable header could be set up to allow the user to change the representation of the data in the view related to the header. \sa setClickable()*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -