📄 q3header.cpp
字号:
return QString(); QString l = d->labels.value(section); if (!l.isNull()) return l; if (d->nullStringLabels.testBit(section) || qt_qheader_label_return_null_strings) return l; else return QString::number(section + 1);}/*! Returns the icon set for section \a section. If the section does not exist, 0 is returned.*/QIcon *Q3Header::iconSet(int section) const{ if (section < 0 || section >= count()) return 0; return d->icons[section];}/*! \overload Adds a new section with icon \a icon and label text \a s. Returns the index position where the section was added (at the right for horizontal headers, at the bottom for vertical headers). The section's width is set to \a size, unless size is negative in which case the size is calculated taking account of the size of the text.*/int Q3Header::addLabel(const QIcon& icon, const QString &s, int size){ int n = count() + 1; d->icons.resize(n + 1); d->icons.insert(n - 1, new QIcon(icon)); return addLabel(s, size);}/*! Removes section \a section. If the section does not exist, nothing happens.*/void Q3Header::removeLabel(int section){ if (section < 0 || section > count() - 1) return; int index = d->s2i[section]; int n = --d->count; int i; for (i = section; i < n; ++i) { d->sizes[i] = d->sizes[i+1]; d->labels[i] = d->labels[i+1]; d->labels[i+1] = QString(); d->nullStringLabels[i] = d->nullStringLabels[i+1]; d->nullStringLabels[i+1] = 0; d->icons[i] = d->icons[i+1]; d->icons[i+1] = 0; } d->sizes.resize(n); d->positions.resize(n); d->labels.resize(n); d->nullStringLabels.resize(n); d->icons.resize(n); for (i = section; i < n; ++i) d->s2i[i] = d->s2i[i+1]; d->s2i.resize(n); if (updatesEnabled()) { for (i = 0; i < n; ++i) if (d->s2i[i] > index) --d->s2i[i]; } for (i = index; i < n; ++i) d->i2s[i] = d->i2s[i+1]; d->i2s.resize(n); if (updatesEnabled()) { for (i = 0; i < n; ++i) if (d->i2s[i] > section) --d->i2s[i]; } if (updatesEnabled()) { updateGeometry(); calculatePositions(); update(); }}QSize Q3Header::sectionSizeHint(int section, const QFontMetrics& fm) const{ int iw = 0; int ih = 0; if (d->icons[section] != 0) { QSize isize = d->icons[section]->pixmap(style()->pixelMetric(QStyle::PM_SmallIconSize), QIcon::Normal).size(); iw = isize.width() + 2; ih = isize.height(); } QRect bound; QString label = d->labels[section]; if (!label.isNull() || d->nullStringLabels.testBit(section)) { int lines = label.count(QLatin1Char('\n')) + 1; int w = 0; if (lines > 1) { bound.setHeight(fm.height() + fm.lineSpacing() * (lines - 1)); QStringList list = label.split(QLatin1Char('\n')); for (int i=0; i < list.count(); ++i) { int tmpw = fm.width(list.at(i)); w = QMAX(w, tmpw); } } else { bound.setHeight(fm.height()); w = fm.width(label); } bound.setWidth(w); } int arrowWidth = 0; if (d->sortSection == section) arrowWidth = ((orient == Qt::Horizontal ? height() : width()) / 2) + 8; int height = qMax(bound.height() + 2, ih) + 4; int width = bound.width() + style()->pixelMetric(QStyle::PM_HeaderMargin) * 4 + iw + arrowWidth; return QSize(width, height);}/* Sets d->sizes[\a section] to a bounding rect based on its size hint and font metrics, but constrained by \a size. It also updates d->height.*/void Q3Header::setSectionSizeAndHeight(int section, int size){ QSize sz = sectionSizeHint(section, fontMetrics()); if (size < 0) { if (d->sizes[section] < 0) d->sizes[section] = (orient == Qt::Horizontal) ? sz.width() : sz.height(); } else { d->sizes[section] = size; } int newHeight = (orient == Qt::Horizontal) ? sz.height() : sz.width(); if (newHeight > d->height) { d->height = newHeight; } else if (newHeight < d->height) { /* We could be smarter, but we aren't. This makes a difference only for users with many columns and '\n's in their headers at the same time. */ d->heightDirty = true; }}/*! Adds a new section with label text \a s. Returns the index position where the section was added (at the right for horizontal headers, at the bottom for vertical headers). The section's width is set to \a size. If \a size \< 0, an appropriate size for the text \a s is chosen.*/int Q3Header::addLabel(const QString &s, int size){ int n = ++d->count; if ((int)d->icons.size() < n ) d->icons.resize(n); if ((int)d->sizes.size() < n ) { d->labels.resize(n); d->nullStringLabels.resize(n); d->sizes.resize(n); d->positions.resize(n); d->i2s.resize(n); d->s2i.resize(n); d->clicks.resize(n); d->resize.resize(n); } int section = d->count - 1; if (!d->is_a_table_header || !s.isNull()) { d->labels.insert(section, s); d->nullStringLabels.setBit(section, s.isNull()); } if (size >= 0 && s.isNull() && d->is_a_table_header) { d->sizes[section] = size; } else { d->sizes[section] = -1; setSectionSizeAndHeight(section, size); } int index = section; d->positions[index] = d->lastPos; d->s2i[section] = index; d->i2s[index] = section; d->clicks.setBit(section, d->clicks_default); d->resize.setBit(section, d->resize_default); if (updatesEnabled()) { updateGeometry(); calculatePositions(); update(); } return index;}void Q3Header::resizeArrays(int size){ d->icons.resize(size); d->labels.resize(size); d->nullStringLabels.resize(size); d->sizes.resize(size); d->positions.resize(size); d->i2s.resize(size); d->s2i.resize(size); d->clicks.resize(size); d->resize.resize(size);}void Q3Header::setIsATableHeader(bool b){ d->is_a_table_header = b;}/*! \reimp */QSize Q3Header::sizeHint() const{ int width; int height; ensurePolished(); QFontMetrics fm = fontMetrics(); if (d->heightDirty) { d->height = fm.lineSpacing() + 6; for (int i = 0; i < count(); i++) { int h = orient == Qt::Horizontal ? sectionSizeHint(i, fm).height() : sectionSizeHint(i, fm).width(); d->height = qMax(d->height, h); } d->heightDirty = false; } if (orient == Qt::Horizontal) { height = fm.lineSpacing() + 6; width = 0; height = qMax(height, d->height); for (int i = 0; i < count(); i++) width += d->sizes[i]; } else { width = fm.width(QLatin1Char(' ')); height = 0; width = qMax(width, d->height); for (int i = 0; i < count(); i++) height += d->sizes[i]; } QStyleOptionHeader opt = getStyleOption(this, 0); return style()->sizeFromContents(QStyle::CT_Q3Header, &opt, QSize(width, height), this).expandedTo(QApplication::globalStrut());}/*! \property Q3Header::offset \brief the header's left-most (or top-most) visible pixel Setting this property will scroll the header so that \e offset becomes the left-most (or top-most for vertical headers) visible pixel.*/int Q3Header::offset() const{ if (reverse()) return d->lastPos - width() - offs; return offs;}void Q3Header::setOffset(int x){ int oldOff = offset(); offs = x; if(d->lastPos < (orient == Qt::Horizontal ? width() : height())) offs = 0; else if (reverse()) offs = d->lastPos - width() - x; if (orient == Qt::Horizontal) scroll(oldOff-offset(), 0); else scroll(0, oldOff-offset());}/* Returns the position of actual division line \a i in widget coordinates. May return a position outside the widget. Note that the last division line is numbered count(). (There is one more line than the number of sections).*/int Q3Header::pPos(int i) const{ int pos; if (i == count()) pos = d->lastPos; else pos = d->positions[i]; if (reverse()) pos = d->lastPos - pos; return pos - offset();}/* Returns the size of the section at index position \a i.*/int Q3Header::pSize(int i) const{ return d->sizes[d->i2s[i]];}/*! Use mapToSection() instead. Translates from actual index \a a (index at which the section is displayed) to logical index of the section. Returns -1 if \a a is outside the legal range. \sa mapToActual()*/int Q3Header::mapToLogical(int a) const{ return mapToSection(a);}/*! Use mapToIndex() instead. Translates from logical index \a l to actual index (index at which the section \a l is displayed) . Returns -1 if \a l is outside the legal range. \sa mapToLogical()*/int Q3Header::mapToActual(int l) const{ return mapToIndex(l);}/*! Use resizeSection() instead. Sets the size of the section \a section to \a s pixels. \warning does not repaint or send out signals*/void Q3Header::setCellSize(int section, int s){ if (section < 0 || section >= count()) return; d->sizes[section] = s; if (updatesEnabled()) calculatePositions(); else d->positionsDirty = true;}/*! If \a enable is true the user may resize section \a section; otherwise the section may not be manually resized. If \a section is negative (the default) then the \a enable value is set for all existing sections and will be applied to any new sections that are added. Example: \code // Allow resizing of all current and future sections header->setResizeEnabled(true); // Disable resizing of section 3, (the fourth section added) header->setResizeEnabled(false, 3); \endcode If the user resizes a section, a sizeChange() signal is emitted. \sa setMovingEnabled() setClickEnabled() setTracking()*/void Q3Header::setResizeEnabled(bool enable, int section){ if (section < 0) { d->resize.fill(enable); // and future ones... d->resize_default = enable; } else if (section < count()) { d->resize[section] = enable; }}/*! \property Q3Header::moving \brief whether the header sections can be moved If this property is true (the default) the user can move sections. If the user moves a section the indexChange() signal is emitted. \sa setClickEnabled(), setResizeEnabled()*/void Q3Header::setMovingEnabled(bool enable){ d->move = enable;}/*! If \a enable is true, any clicks on section \a section will result in clicked() signals being emitted; otherwise the section will ignore clicks. If \a section is -1 (the default) then the \a enable value is set for all existing sections and will be applied to any new sections that are added. \sa setMovingEnabled(), setResizeEnabled()*/void Q3Header::setClickEnabled(bool enable, int section){ if (section < 0) { d->clicks.fill(enable); // and future ones... d->clicks_default = enable; } else if (section < count()) { d->clicks[section] = enable; }}/*! Paints the section at position \a index, inside rectangle \a fr (which uses widget coordinates) using painter \a p. Calls paintSectionLabel().*/void Q3Header::paintSection(QPainter *p, int index, const QRect& fr){ int section = mapToSection(index); QStyleOptionHeader opt = getStyleOption(this, section); opt.state |= QStyle::State_Raised; opt.rect = fr; if (section < 0) { style()->drawControl(QStyle::CE_Header, &opt, p, this); return; } if (sectionSize(section) <= 0) return; opt.state = (orient == Qt::Horizontal ? QStyle::State_Horizontal : QStyle::State_None); if (d->sortSection == section) opt.sortIndicator = d->sortDirection ? QStyleOptionHeader::SortDown : QStyleOptionHeader::SortUp; if (isEnabled()) opt.state |= QStyle::State_Enabled; if (isClickEnabled(section) && (state == Pressed || state == Moving) && index == handleIdx) opt.state |= QStyle::State_Sunken; //currently pressed if (!(opt.state & QStyle::State_Sunken)) opt.state |= QStyle::State_Raised; p->setBrushOrigin(fr.topLeft()); if (d->clicks[section]) { style()->drawControl(QStyle::CE_Header, &opt, p, this); } else { p->save(); p->setClipRect(fr); // hack to keep styles working opt.rect.setRect(fr.x() + 1, fr.y(), fr.width(), fr.height()); style()->drawControl(QStyle::CE_Header, &opt, p, this); if (orient == Qt::Horizontal) { p->setPen(palette().color(QPalette::Mid)); p->drawLine(fr.x() - 1, fr.y() + fr.height() - 1, fr.x() + fr.width() - 1, fr.y() + fr.height() - 1); p->drawLine(fr.x() + fr.width() - 1, fr.y(), fr.x() + fr.width() - 1, fr.y() + fr.height() - 1); } else { p->setPen(palette().color(QPalette::Mid)); p->drawLine(fr.x() + width() - 1, fr.y(), fr.x() + fr.width() - 1, fr.y() + fr.height() - 1); p->drawLine(fr.x(), fr.y() + fr.height() - 1, fr.x() + fr.width() - 1, fr.y() + fr.height() - 1); p->setPen(palette().color(QPalette::Light)); if (index > 0) p->drawLine(fr.x(), fr.y(), fr.x() + fr.width() - 1, fr.y()); if (index == count() - 1) { p->drawLine(fr.x(), fr.y() + fr.height() - 1, fr.x() + fr.width() - 1, fr.y() + fr.height() - 1); p->setPen(palette().color(QPalette::Mid));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -