📄 qheaderview.cpp
字号:
emit sectionCountChanged(oldCount, 0); } else if (c != count() && c != 0) initializeSections(0, qMax(c - 1, 0)); } else { int r = model()->rowCount(rootIndex()); if (r == 0) { int oldCount = count(); d->clear(); emit sectionCountChanged(oldCount, 0); } else if (r != count() && r != 0) initializeSections(0, qMax(r - 1, 0)); }}/*! \internal*/void QHeaderView::initializeSections(int start, int end){ Q_D(QHeaderView); Q_ASSERT(start >= 0); Q_ASSERT(end >= 0); int oldCount = count(); end += 1; // one past the last item, so we get the end position of the last section d->sections.resize(end + 1); if (!d->logicalIndices.isEmpty()){ d->logicalIndices.resize(end + 1); d->visualIndices.resize(end + 1); for (int i = start; i < end; ++i){ d->logicalIndices[i] = i; d->visualIndices[i] = i; } } int pos = (start > 0 ? d->sections.at(start).position : 0); QHeaderViewPrivate::HeaderSection *sections = d->sections.data() + start; int num = end - start + 1; int size = d->defaultSectionSize; // set resize mode ResizeMode mode = d->globalResizeMode; if (mode == Stretch) d->stretchSections += num; else if (mode == Custom) d->customSections += num; // unroll loop - to initialize the arrays as fast as possible while (num >= 4) { sections[0].hidden = false; sections[0].mode = mode; sections[0].position = pos; pos += size; sections[1].hidden = false; sections[1].mode = mode; sections[1].position = pos; pos += size; sections[2].hidden = false; sections[2].mode = mode; sections[2].position = pos; pos += size; sections[3].hidden = false; sections[3].mode = mode; sections[3].position = pos; pos += size; sections += 4; num -= 4; } if (num > 0) { sections[0].hidden = false; sections[0].mode = mode; sections[0].position = pos; pos += size; if (num > 1) { sections[1].hidden = false; sections[1].mode = mode; sections[1].position = pos; pos += size; if (num > 2) { sections[2].hidden = false; sections[2].mode = mode; sections[2].position = pos; pos += size; } } } d->invalidateCachedSizeHint(); emit sectionCountChanged(oldCount, count()); d->viewport->update();}/*! \reimp*/void QHeaderView::currentChanged(const QModelIndex ¤t, const QModelIndex &old){ Q_D(QHeaderView); if (d->orientation == Qt::Horizontal && current.column() != old.column()) { if (old.isValid() && old.parent() == rootIndex()) d->setDirtyRegion(QRect(sectionViewportPosition(old.column()), 0, sectionSize(old.column()), d->viewport->height())); if (current.isValid() && current.parent() == rootIndex()) d->setDirtyRegion(QRect(sectionViewportPosition(current.column()), 0, sectionSize(current.column()), d->viewport->height())); } else if (d->orientation == Qt::Vertical && current.row() != old.row()) { if (old.isValid() && old.parent() == rootIndex()) d->setDirtyRegion(QRect(0, sectionViewportPosition(old.row()), d->viewport->width(), sectionSize(old.row()))); if (current.isValid() && current.parent() == rootIndex()) d->setDirtyRegion(QRect(0, sectionViewportPosition(current.row()), d->viewport->width(), sectionSize(current.row()))); } d->updateDirtyRegion();}/*! \reimp*/bool QHeaderView::event(QEvent *e){ Q_D(QHeaderView); switch (e->type()) { case QEvent::HoverEnter: { QHoverEvent *he = static_cast<QHoverEvent*>(e); d->hover = logicalIndexAt(he->pos()); if (d->hover != -1) updateSection(d->hover); break; } case QEvent::HoverLeave: { if (d->hover != -1) updateSection(d->hover); d->hover = -1; break; } case QEvent::HoverMove: { QHoverEvent *he = static_cast<QHoverEvent*>(e); int oldHover = d->hover; d->hover = logicalIndexAt(he->pos()); if (d->hover != oldHover) { if (oldHover != -1) updateSection(oldHover); if (d->hover != -1) updateSection(d->hover); } break; } default: break; } return QAbstractItemView::event(e);}/*! \reimp*/void QHeaderView::paintEvent(QPaintEvent *e){ Q_D(QHeaderView); if (d->model == 0) return; QPainter painter(d->viewport); const QPoint offset = d->scrollDelayOffset; QRect area = e->rect(); area.translate(offset); int start, end; if (orientation() == Qt::Horizontal) { start = visualIndexAt(area.left()); end = visualIndexAt(area.right() - 1); } else { start = visualIndexAt(area.top()); end = visualIndexAt(area.bottom() - 1); } if (d->reverse()) { start = (start == -1 ? count() - 1 : start); end = (end == -1 ? 0 : end); } else { start = (start == -1 ? 0 : start); end = (end == -1 ? count() - 1 : end); } int tmp = start; start = qMin(start, end); end = qMax(tmp, end); if (count() == 0) return; d->prepareSectionSelected(); // clear and resize the bit array const QVector<QHeaderViewPrivate::HeaderSection> sections = d->sections; QRect rect; int logical; bool highlight = false; const int width = d->viewport->width(); const int height = d->viewport->height(); const bool active = isActiveWindow(); for (int i = start; i <= end; ++i) { if (sections.at(i).hidden) continue; painter.save(); logical = logicalIndex(i); if (orientation() == Qt::Horizontal) { rect.setRect(sectionViewportPosition(logical), 0, sectionSize(logical), height); highlight = d->selectionModel->columnIntersectsSelection(logical, rootIndex()); } else { rect.setRect(0, sectionViewportPosition(logical), width, sectionSize(logical)); highlight = d->selectionModel->rowIntersectsSelection(logical, rootIndex()); } rect.translate(offset); QVariant variant = d->model->headerData(logical, orientation(), Qt::FontRole); if (variant.isValid() && qVariantCanConvert<QFont>(variant)) { QFont sectionFont = qvariant_cast<QFont>(variant); if (highlight) sectionFont.setBold(true); painter.setFont(sectionFont); } paintSection(&painter, rect, logical); painter.restore(); } // Paint the area beyond where there are indexes if (d->reverse()) { if (rect.left() > area.left()) painter.fillRect(area.left(), 0, rect.left() - area.left(), height, palette().background()); } else if (rect.right() < area.right()) { // paint to the right painter.fillRect(rect.right() + 1, 0, area.right() - rect.right(), height, palette().background()); } else if (rect.bottom() < area.bottom()) { painter.fillRect(0, rect.bottom() + 1, width, height - rect.bottom() - 1, palette().background()); }}/*! \reimp*/void QHeaderView::mousePressEvent(QMouseEvent *e){ Q_D(QHeaderView); if (d->state != QHeaderViewPrivate::NoState || e->button() != Qt::LeftButton) return; int pos = orientation() == Qt::Horizontal ? e->x() : e->y(); int handle = d->sectionHandleAt(pos); while (handle > -1 && isSectionHidden(handle)) --handle; if (handle == -1) { d->pressed = logicalIndexAt(pos); if (d->movableSections) { d->section = d->target = d->pressed; if (d->section == -1) return; d->state = QHeaderViewPrivate::MoveSection; d->setupSectionIndicator(d->section, pos); } if (d->clickableSections && d->pressed != -1) { updateSection(d->pressed); emit sectionPressed(d->pressed); } } else if (resizeMode(handle) == Interactive) { d->state = QHeaderViewPrivate::ResizeSection; d->section = handle; } d->firstPos = pos; d->lastPos = pos;}/*! \reimp*/void QHeaderView::mouseMoveEvent(QMouseEvent *e){ Q_D(QHeaderView); int pos = orientation() == Qt::Horizontal ? e->x() : e->y(); if (pos < 0) return; switch (d->state) { case QHeaderViewPrivate::ResizeSection: { int delta = d->reverse() ? d->lastPos - pos : pos - d->lastPos; int size = sectionSize(d->section) + delta; QSize strut = QApplication::globalStrut(); int minimum = orientation() == Qt::Horizontal ? strut.width() : strut.height(); if (size > minimum) { resizeSection(d->section, size); d->lastPos = (orientation() == Qt::Horizontal ? e->x() : e->y()); } return; } case QHeaderViewPrivate::MoveSection: { if (qAbs(pos - d->firstPos) >= QApplication::startDragDistance()) { int indicatorCenter = (orientation() == Qt::Horizontal ? d->sectionIndicator->width() : d->sectionIndicator->height()) / 2; int centerOffset = indicatorCenter - d->sectionIndicatorOffset; // This will drop the moved section to the position under the center of the indicator. // If centerOffset is 0, the section will be moved to the position of the mouse cursor. int visual = visualIndexAt(pos + centerOffset); if (visual == -1) return; d->target = d->logicalIndex(visual); d->updateSectionIndicator(d->section, pos); } else { int visual = visualIndexAt(d->firstPos); if (visual == -1) return; d->target = d->logicalIndex(visual); d->updateSectionIndicator(d->section, d->firstPos); } return; } case QHeaderViewPrivate::NoState: {#ifndef QT_NO_CURSOR int handle = d->sectionHandleAt(pos); if (handle != -1 && resizeMode(handle) == Interactive && !(visualIndex(handle) == count() - 1 && stretchLastSection())) setCursor(orientation() == Qt::Horizontal ? Qt::SplitHCursor : Qt::SplitVCursor); else setCursor(Qt::ArrowCursor);#endif return; } }}/*! \reimp*/void QHeaderView::mouseReleaseEvent(QMouseEvent *e){ Q_D(QHeaderView); int pos = orientation() == Qt::Horizontal ? e->x() : e->y(); switch (d->state) { case QHeaderViewPrivate::MoveSection: if (!d->sectionIndicator->isHidden()) { // moving int from = visualIndex(d->section); Q_ASSERT(from != -1); int to = visualIndex(d->target); Q_ASSERT(to != -1); moveSection(from, to); d->section = d->target = -1; d->updateSectionIndicator(d->section, pos); break; } // not moving case QHeaderViewPrivate::NoState: if (d->clickableSections) { int section = logicalIndexAt(pos); if (section != -1 && section == d->pressed) emit sectionClicked(logicalIndexAt(pos)); if (d->pressed != -1) updateSection(d->pressed); } break; case QHeaderViewPrivate::ResizeSection: break; } d->state = QHeaderViewPrivate::NoState; d->pressed = -1;}/*! \reimp*/void QHeaderView::mouseDoubleClickEvent(QMouseEvent *e){ Q_D(QHeaderView); int pos = orientation() == Qt::Horizontal ? e->x() : e->y(); int handle = d->sectionHandleAt(pos); while (handle > -1 && isSectionHidden(handle)) handle--; if (handle > -1 && resizeMode(handle) == Interactive) emit sectionHandleDoubleClicked(handle); else emit sectionDoubleClicked(logicalIndexAt(e->pos()));}/*! \reimp*/bool QHeaderView::viewportEvent(QEvent *e){ Q_D(QHeaderView); switch (e->type()) {#ifndef QT_NO_TOOLTIP case QEvent::ToolTip: { if (!isActiveWindow()) break; QHelpEvent *he = static_cast<QHelpEvent*>(e); int logical = logicalIndexAt(he->pos()); if (logical != -1) { QVariant variant = model()->headerData(logical, orientation(), Qt::ToolTipRole); if (variant.isValid()) { QToolTip::showText(he->globalPos(), variant.toString(), this); return true; } } break; }#endif#ifndef QT_NO_WHATSTHIS case QEvent::QueryWhatsThis: { QHelpEvent *he = static_cast<QHelpEvent*>(e); int logical = logicalIndexAt(he->pos()); if (logical != -1 && model()->headerData(logical, orientation(), Qt::WhatsThisRole).isValid()) return true; break; } case QEvent::WhatsThis: { QHelpEvent *he = static_cast<QHelpEvent*>(e); int logical = logicalIndexAt(he->pos()); if (logical != -1) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -