📄 qitemdelegate.cpp
字号:
painter->setPen(option.palette.color(cg, QPalette::Text)); } if (option.state & QStyle::State_Editing) { painter->save(); painter->setPen(option.palette.color(cg, QPalette::Text)); painter->drawRect(rect.adjusted(0, 0, -1, -1)); painter->restore(); } const QStyleOptionViewItemV3 opt = option; const QWidget *widget = d->widget(option); QStyle *style = widget ? widget->style() : QApplication::style(); const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1; QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding const bool wrapText = opt.features & QStyleOptionViewItemV2::WrapText; d->textOption.setWrapMode(wrapText ? QTextOption::WordWrap : QTextOption::ManualWrap); d->textOption.setTextDirection(option.direction); d->textOption.setAlignment(QStyle::visualAlignment(option.direction, option.displayAlignment)); d->textLayout.setTextOption(d->textOption); d->textLayout.setFont(option.font); d->textLayout.setText(QItemDelegatePrivate::replaceNewLine(text)); QSizeF textLayoutSize = d->doTextLayout(textRect.width()); if (textRect.width() < textLayoutSize.width() || textRect.height() < textLayoutSize.height()) { QString elided; int start = 0; int end = text.indexOf(QChar::LineSeparator, start); if (end == -1) { elided += option.fontMetrics.elidedText(text, option.textElideMode, textRect.width()); } else { while (end != -1) { elided += option.fontMetrics.elidedText(text.mid(start, end - start), option.textElideMode, textRect.width()); elided += QChar::LineSeparator; start = end + 1; end = text.indexOf(QChar::LineSeparator, start); } //let's add the last line (after the last QChar::LineSeparator) elided += option.fontMetrics.elidedText(text.mid(start), option.textElideMode, textRect.width()); if (end != -1) elided += QChar::LineSeparator; } d->textLayout.setText(elided); textLayoutSize = d->doTextLayout(textRect.width()); } const QSize layoutSize(textRect.width(), int(textLayoutSize.height())); const QRect layoutRect = QStyle::alignedRect(option.direction, option.displayAlignment, layoutSize, textRect); // if we still overflow even after eliding the text, enable clipping if (!hasClipping() && (textRect.width() < textLayoutSize.width() || textRect.height() < textLayoutSize.height())) { painter->save(); painter->setClipRect(layoutRect); d->textLayout.draw(painter, layoutRect.topLeft(), QVector<QTextLayout::FormatRange>(), layoutRect); painter->restore(); } else { d->textLayout.draw(painter, layoutRect.topLeft(), QVector<QTextLayout::FormatRange>(), layoutRect); }}/*! Renders the decoration \a pixmap within the rectangle specified by \a rect using the given \a painter and style \a option.*/void QItemDelegate::drawDecoration(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QPixmap &pixmap) const{ Q_D(const QItemDelegate); // if we have an icon, we ignore the pixmap if (!d->tmp.icon.isNull()) { d->tmp.icon.paint(painter, rect, option.decorationAlignment, d->tmp.mode, d->tmp.state); return; } if (pixmap.isNull() || !rect.isValid()) return; QPoint p = QStyle::alignedRect(option.direction, option.decorationAlignment, pixmap.size(), rect).topLeft(); if (option.state & QStyle::State_Selected) { QPixmap *pm = selected(pixmap, option.palette, option.state & QStyle::State_Enabled); painter->drawPixmap(p, *pm); } else { painter->drawPixmap(p, pixmap); }}/*! Renders the region within the rectangle specified by \a rect, indicating that it has the focus, using the given \a painter and style \a option.*/void QItemDelegate::drawFocus(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect) const{ Q_D(const QItemDelegate); if ((option.state & QStyle::State_HasFocus) == 0 || !rect.isValid()) return; QStyleOptionFocusRect o; o.QStyleOption::operator=(option); o.rect = rect; o.state |= QStyle::State_KeyboardFocusChange; o.state |= QStyle::State_Item; QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? QPalette::Normal : QPalette::Disabled; o.backgroundColor = option.palette.color(cg, (option.state & QStyle::State_Selected) ? QPalette::Highlight : QPalette::Window); const QWidget *widget = d->widget(option); QStyle *style = widget ? widget->style() : QApplication::style(); style->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter, widget);}/*! Renders a check indicator within the rectangle specified by \a rect, using the given \a painter and style \a option, using the given \a state.*/void QItemDelegate::drawCheck(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const{ Q_D(const QItemDelegate); if (!rect.isValid()) return; QStyleOptionViewItem opt(option); opt.rect = rect; opt.state = opt.state & ~QStyle::State_HasFocus; switch (state) { case Qt::Unchecked: opt.state |= QStyle::State_Off; break; case Qt::PartiallyChecked: opt.state |= QStyle::State_NoChange; break; case Qt::Checked: opt.state |= QStyle::State_On; break; } const QWidget *widget = d->widget(option); QStyle *style = widget ? widget->style() : QApplication::style(); style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter, widget);}/*! \since 4.2 Renders the item background for the given \a index, using the given \a painter and style \a option.*/void QItemDelegate::drawBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ if (option.showDecorationSelected && (option.state & QStyle::State_Selected)) { QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled; if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) cg = QPalette::Inactive; painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight)); } else { QVariant value = index.data(Qt::BackgroundRole); if (qVariantCanConvert<QBrush>(value)) { QPointF oldBO = painter->brushOrigin(); painter->setBrushOrigin(option.rect.topLeft()); painter->fillRect(option.rect, qvariant_cast<QBrush>(value)); painter->setBrushOrigin(oldBO); } }}/*! \internal*/void QItemDelegate::doLayout(const QStyleOptionViewItem &option, QRect *checkRect, QRect *pixmapRect, QRect *textRect, bool hint) const{ Q_ASSERT(checkRect && pixmapRect && textRect); Q_D(const QItemDelegate); const QWidget *widget = d->widget(option); QStyle *style = widget ? widget->style() : QApplication::style(); const bool hasCheck = checkRect->isValid(); const bool hasPixmap = pixmapRect->isValid(); const bool hasText = textRect->isValid(); const int textMargin = hasText ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0; const int pixmapMargin = hasPixmap ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0; const int checkMargin = hasCheck ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0; int x = option.rect.left(); int y = option.rect.top(); int w, h; textRect->adjust(-textMargin, 0, textMargin, 0); // add width padding if (textRect->height() == 0 && !hasPixmap) textRect->setHeight(option.fontMetrics.height()); QSize pm(0, 0); if (hasPixmap) { pm = pixmapRect->size(); pm.rwidth() += 2 * pixmapMargin; } if (hint) { h = qMax(checkRect->height(), qMax(textRect->height(), pm.height())); if (option.decorationPosition == QStyleOptionViewItem::Left || option.decorationPosition == QStyleOptionViewItem::Right) { w = textRect->width() + pm.width(); } else { w = qMax(textRect->width(), pm.width()); } } else { w = option.rect.width(); h = option.rect.height(); } int cw = 0; QRect check; if (hasCheck) { cw = checkRect->width() + 2 * checkMargin; if (hint) w += cw; if (option.direction == Qt::RightToLeft) { check.setRect(x + w - cw, y, cw, h); } else { check.setRect(x + checkMargin, y, cw, h); } } // at this point w should be the *total* width QRect display; QRect decoration; switch (option.decorationPosition) { case QStyleOptionViewItem::Top: { if (hasPixmap) pm.setHeight(pm.height() + pixmapMargin); // add space h = hint ? textRect->height() : h - pm.height(); if (option.direction == Qt::RightToLeft) { decoration.setRect(x, y, w - cw, pm.height()); display.setRect(x, y + pm.height(), w - cw, h); } else { decoration.setRect(x + cw, y, w - cw, pm.height()); display.setRect(x + cw, y + pm.height(), w - cw, h); } break; } case QStyleOptionViewItem::Bottom: { if (hasText) textRect->setHeight(textRect->height() + textMargin); // add space h = hint ? textRect->height() + pm.height() : h; if (option.direction == Qt::RightToLeft) { display.setRect(x, y, w - cw, textRect->height()); decoration.setRect(x, y + textRect->height(), w - cw, h - textRect->height()); } else { display.setRect(x + cw, y, w - cw, textRect->height()); decoration.setRect(x + cw, y + textRect->height(), w - cw, h - textRect->height()); } break; } case QStyleOptionViewItem::Left: { if (option.direction == Qt::LeftToRight) { decoration.setRect(x + cw, y, pm.width(), h); display.setRect(decoration.right() + 1, y, w - pm.width() - cw, h); } else { display.setRect(x, y, w - pm.width() - cw, h); decoration.setRect(display.right() + 1, y, pm.width(), h); } break; } case QStyleOptionViewItem::Right: { if (option.direction == Qt::LeftToRight) { display.setRect(x + cw, y, w - pm.width() - cw, h); decoration.setRect(display.right() + 1, y, pm.width(), h); } else { decoration.setRect(x, y, pm.width(), h); display.setRect(decoration.right() + 1, y, w - pm.width() - cw, h); } break; } default: qWarning("doLayout: decoration position is invalid"); decoration = *pixmapRect; break; } if (!hint) { // we only need to do the internal layout if we are going to paint *checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter, checkRect->size(), check); *pixmapRect = QStyle::alignedRect(option.direction, option.decorationAlignment, pixmapRect->size(), decoration); // the text takes up all available space, unless the decoration is not shown as selected if (option.showDecorationSelected) *textRect = display; else *textRect = QStyle::alignedRect(option.direction, option.displayAlignment, textRect->size().boundedTo(display.size()), display); } else { *checkRect = check; *pixmapRect = decoration; *textRect = display; }}/*! \internal Returns the pixmap used to decorate the root of the item view. The style \a option controls the appearance of the root; the \a variant refers to the data associated with an item.*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -