📄 qheaderview.cpp
字号:
QString whatsthis = model()->headerData(logical, orientation(), Qt::WhatsThisRole).toString(); QWhatsThis::showText(he->globalPos(), whatsthis, this); return true; } break; }#endif // QT_NO_WHATSTHIS#ifndef QT_NO_STATUSTIP case QEvent::StatusTip: { QHelpEvent *he = static_cast<QHelpEvent*>(e); int logical = logicalIndexAt(he->pos()); if (logical != -1) { QString statustip = model()->headerData(logical, orientation(), Qt::StatusTipRole).toString(); if (!statustip.isEmpty()) setStatusTip(statustip); } return true; }#endif // QT_NO_STATUSTIP case QEvent::Hide: case QEvent::Show: case QEvent::FontChange: resizeSections(); if (QAbstractItemView *par = ::qobject_cast<QAbstractItemView*>(parent())) { int slot = par->metaObject()->indexOfSlot("updateGeometries()"); QApplication::postEvent(par, new QMetaCallEvent(slot)); } break; case QEvent::ContextMenu: { d->state = QHeaderViewPrivate::NoState; d->pressed =d->section = d->target = -1; d->updateSectionIndicator(d->section, -1); } default: break; } return QAbstractItemView::viewportEvent(e);}/*! Paints the section specified by the given \a logicalIndex, using the given \a painter and \a rect. You normally would not need to use this function.*/void QHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const{ Q_D(const QHeaderView); if (!d->model || !rect.isValid()) return; // get the state of the section QStyleOptionHeader opt = d->getStyleOption(); QStyle::State state = QStyle::State_None; if (isEnabled()) state |= QStyle::State_Enabled; if (window()->isActiveWindow()) state |= QStyle::State_Active; if (d->clickableSections) { if (logicalIndex == d->hover) state |= QStyle::State_MouseOver; if (logicalIndex == d->pressed) state |= QStyle::State_Sunken; else if (d->highlightSelected && d->isSectionSelected(logicalIndex)) state |= QStyle::State_On | QStyle::State_Sunken; } if (isSortIndicatorShown() && sortIndicatorSection() == logicalIndex) opt.sortIndicator = (sortIndicatorOrder() == Qt::AscendingOrder) ? QStyleOptionHeader::SortDown : QStyleOptionHeader::SortUp; // setup the style options structure QVariant textAlignment = d->model->headerData(logicalIndex, orientation(), Qt::TextAlignmentRole); opt.rect = rect; opt.section = logicalIndex; opt.state |= state; opt.textAlignment = Qt::Alignment(textAlignment.isValid() ? Qt::Alignment(textAlignment.toInt()) : d->defaultAlignment); opt.iconAlignment = Qt::AlignVCenter; opt.text = d->model->headerData(logicalIndex, orientation(), Qt::DisplayRole).toString(); QVariant variant = d->model->headerData(logicalIndex, orientation(), Qt::DecorationRole); opt.icon = qvariant_cast<QIcon>(variant); if (opt.icon.isNull()) opt.icon = qvariant_cast<QPixmap>(variant); QVariant textColor = d->model->headerData(logicalIndex, orientation(), Qt::TextColorRole); if (textColor.isValid() && qvariant_cast<QColor>(textColor).isValid()) opt.palette.setColor(QPalette::ButtonText, qvariant_cast<QColor>(textColor)); // the section position int visual = visualIndex(logicalIndex); Q_ASSERT(visual != -1); if (count() == 1) opt.position = QStyleOptionHeader::OnlyOneSection; else if (visual == 0) opt.position = QStyleOptionHeader::Beginning; else if (visual == count() - 1) opt.position = QStyleOptionHeader::End; else opt.position = QStyleOptionHeader::Middle; opt.orientation = d->orientation; // the selected position bool previousSelected = d->isSectionSelected(this->logicalIndex(visual - 1)); bool nextSelected = d->isSectionSelected(this->logicalIndex(visual + 1)); if (previousSelected && nextSelected) opt.selectedPosition = QStyleOptionHeader::NextAndPreviousAreSelected; else if (previousSelected) opt.selectedPosition = QStyleOptionHeader::PreviousIsSelected; else if (nextSelected) opt.selectedPosition = QStyleOptionHeader::NextIsSelected; else opt.selectedPosition = QStyleOptionHeader::NotAdjacent; // draw the section style()->drawControl(QStyle::CE_Header, &opt, painter, this);}/*! Returns the size of the contents of the section specified by the give \a logicalIndex. \sa defaultSectionSize()*/QSize QHeaderView::sectionSizeFromContents(int logicalIndex) const{ Q_D(const QHeaderView); Q_ASSERT(logicalIndex >= 0); if (!d->model) return QSize(); QSize size(100, 30); QStyleOptionHeader opt = d->getStyleOption(); QFont fnt = font(); fnt.setBold(true); opt.fontMetrics = QFontMetrics(fnt); // do the metrics with a bold font opt.text = d->model->headerData(logicalIndex, orientation(), Qt::DisplayRole).toString(); QVariant variant = d->model->headerData(logicalIndex, orientation(), Qt::DecorationRole); opt.icon = qvariant_cast<QIcon>(variant); if (opt.icon.isNull()) opt.icon = qvariant_cast<QPixmap>(variant); size = style()->sizeFromContents(QStyle::CT_HeaderSection, &opt, size, this); if (isSortIndicatorShown() && sortIndicatorSection() == logicalIndex) { int margin = style()->pixelMetric(QStyle::PM_HeaderMargin); if (orientation() == Qt::Horizontal) size.rwidth() += size.height() + margin; else size.rheight() += size.width() + margin; } return size;}/*! Returns the horizontal offset of the header. This is 0 for vertical headers. \sa offset()*/int QHeaderView::horizontalOffset() const{ Q_D(const QHeaderView); if (orientation() == Qt::Horizontal) return d->offset; return 0;}/*! Returns the vertical offset of the header. This is 0 for horizontal headers. \sa offset()*/int QHeaderView::verticalOffset() const{ Q_D(const QHeaderView); if (orientation() == Qt::Vertical) return d->offset; return 0;}/*! \reimp \internal*/void QHeaderView::updateGeometries(){ Q_D(QHeaderView); d->layoutChildren(); if (d->hasAutoResizeSections()) resizeSections();}/*! \reimp \internal*/void QHeaderView::scrollContentsBy(int dx, int dy){ Q_D(QHeaderView); d->scrollDirtyRegion(dx, dy);}/*! \reimp \internal*/void QHeaderView::dataChanged(const QModelIndex &, const QModelIndex &){ Q_D(QHeaderView); d->invalidateCachedSizeHint();}/*! \reimp \internal Empty implementation because the header doesn't show QModelIndex items.*/void QHeaderView::rowsInserted(const QModelIndex &, int, int){ // do nothing}/*! \reimp \internal Empty implementation because the header doesn't show QModelIndex items.*/QRect QHeaderView::visualRect(const QModelIndex &) const{ return QRect();}/*! \reimp \internal Empty implementation because the header doesn't show QModelIndex items.*/void QHeaderView::scrollTo(const QModelIndex &, ScrollHint){ // do nothing - the header only displays sections}/*! \reimp \internal Empty implementation because the header doesn't show QModelIndex items.*/QModelIndex QHeaderView::indexAt(const QPoint &) const{ return QModelIndex();}/*! \reimp \internal Empty implementation because the header doesn't show QModelIndex items.*/bool QHeaderView::isIndexHidden(const QModelIndex &) const{ return true; // the header view has no items, just sections}/*! \reimp \internal Empty implementation because the header doesn't show QModelIndex items.*/QModelIndex QHeaderView::moveCursor(CursorAction, Qt::KeyboardModifiers){ return QModelIndex();}/*! \reimp Selects the items in the given \a rect according to the specified \a flags. The base class implementation does nothing.*/void QHeaderView::setSelection(const QRect&, QItemSelectionModel::SelectionFlags){ // do nothing}/*! \internal*/QRegion QHeaderView::visualRegionForSelection(const QItemSelection &selection) const{ Q_D(const QHeaderView); if (!d->model) return QRegion(); if (orientation() == Qt::Horizontal) { int left = d->model->columnCount(rootIndex()) - 1; int right = 0; int rangeLeft, rangeRight; for (int i = 0; i < selection.count(); ++i) { QItemSelectionRange r = selection.at(i); if (r.parent().isValid() || !r.isValid()) continue; // we only know about toplevel items and we don't want invalid ranges // FIXME an item inside the range may be the leftmost or rightmost rangeLeft = visualIndex(r.left()); if (rangeLeft == -1) // in some cases users may change the selections continue; // before we have a chance to do the layout rangeRight = visualIndex(r.right()); if (rangeRight == -1) // in some cases users may change the selections continue; // before we have a chance to do the layout if (rangeLeft < left) left = rangeLeft; if (rangeRight > right) right = rangeRight; } int logicalLeft = logicalIndex(left); int logicalRight = logicalIndex(right); if (logicalLeft < 0 || logicalLeft >= count() || logicalRight < 0 || logicalRight >= count()) return QRegion(); int leftPos = sectionViewportPosition(logicalLeft); int rightPos = sectionViewportPosition(logicalRight); rightPos += sectionSize(logicalRight); return QRect(leftPos, 0, rightPos - leftPos, height()); } // orientation() == Qt::Vertical int top = d->model->rowCount(rootIndex()) - 1; int bottom = 0; int rangeTop, rangeBottom; for (int i = 0; i < selection.count(); ++i) { QItemSelectionRange r = selection.at(i); if (r.parent().isValid() || !r.isValid()) continue; // we only know about toplevel items // FIXME an item inside the range may be the leftmost or rightmost rangeTop = visualIndex(r.top()); if (rangeTop == -1) // in some cases users may change the selections continue; // before we have a chance to do the layout rangeBottom = visualIndex(r.bottom()); if (rangeBottom == -1) // in some cases users may change the selections continue; // before we have a chance to do the layout if (rangeTop < top) top = rangeTop; if (rangeBottom > bottom) bottom = rangeBottom; } int logicalTop = logicalIndex(top); int logicalBottom = logicalIndex(bottom); if (logicalTop == -1 || logicalBottom == -1) return QRect(); int topPos = sectionViewportPosition(logicalTop); int bottomPos = sectionViewportPosition(logicalBottom) + sectionSize(logicalBottom); return QRect(0, topPos, width(), bottomPos - topPos);}// private implementationint QHeaderViewPrivate::sectionHandleAt(int position){ Q_Q(QHeaderView); int visual = q->visualIndexAt(position); if (visual == -1) return -1; int log = logicalIndex(visual); int pos = q->sectionViewportPosition(log); int grip = q->style()->pixelMetric(QStyle::PM_HeaderGripMargin); if (reverse()) { if (position < pos + grip) return log; if (visual > 0 && position > pos + q->sectionSize(log) - grip) return q->logicalIndex(visual - 1); } else { if (visual > 0 && position < pos + grip) return q->logicalIndex(visual - 1); if (position > pos + q->sectionSize(log) - grip) return log; } return -1;}void QHeaderViewPrivate::setupSectionIndicator(int section, int position){ Q_Q(QHeaderView); if (!sectionIndicator) { sectionIndicator = new QLabel(viewport); sectionIndicator->setWindowOpacity(0.75); } int x, y, w, h; int p = q->sectionViewportPosition(section); if (orientation == Qt::Horizontal) { x = p; y = 0; w = q->sectionSize(section); h = viewport->height(); } else { x = 0; y = p; w = viewport->width(); h = q->sectionSize(section); } sectionIndicator->resize(w, h); QPixmap pix = QPixmap::grabWidget(viewport, x, y, w, h); sectionIndicator->setPixmap(pix); sectionIndicatorOffset = position - qMax(p, 0);}void QHeaderViewPrivate::upd
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -