📄 qitemdelegate.cpp
字号:
provided by \a option.*/QSize QItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const{ Q_ASSERT(index.isValid()); QVariant value = index.data(Qt::SizeHintRole); if (value.isValid()) return qvariant_cast<QSize>(value); // display value = index.data(Qt::FontRole); QRect textRect; QFont fnt = value.isValid() ? qvariant_cast<QFont>(value) : option.font; // In qt 4.2 there will be a proper option in QStyleOptionViewItem // Not calling fontMetrics.width speeds up the code _significatly_ const QString text = index.data(Qt::DisplayRole).toString(); if (option.rect.width() == -1) { QFontMetrics fontMetrics(fnt); textRect = QRect(0, 0, 0, fontMetrics.lineSpacing() * (text.count(QLatin1Char('\n')) + 1)); } else { const QChar *chr = text.constData(); const QChar *end = chr + text.length(); while (chr != end && *chr != QLatin1Char('\n') && *chr != QLatin1Char('\t') && *chr != QLatin1Char('&')) ++chr; if (chr == end) { QFontMetrics fontMetrics(fnt); textRect = QRect(0, 0, fontMetrics.width(text), fontMetrics.lineSpacing()); } else { QRectF result; qt_format_text(fnt, option.rect, Qt::TextDontPrint|Qt::TextDontClip|Qt::TextExpandTabs, text, &result, 0, 0, 0, 0); textRect = result.toRect(); } } // decoration QRect pixmapRect; if (index.data(Qt::DecorationRole).isValid()) pixmapRect = QRect(0, 0, option.decorationSize.width(), option.decorationSize.height()); // checkbox QRect checkRect = check(option, textRect, index.data(Qt::CheckStateRole)); doLayout(option, &checkRect, &pixmapRect, &textRect, true); return (pixmapRect|textRect|checkRect).size();}/*! Returns the widget used to edit the item specified by \a index for editing. The \a parent widget and style \a option are used to control how the editor widget appears. \sa QAbstractItemDelegate::createEditor()*/QWidget *QItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const{ Q_D(const QItemDelegate); if (!index.isValid()) return 0; QVariant::Type t = index.data(Qt::EditRole).type(); const QItemEditorFactory *factory = d->f; if (factory == 0) factory = QItemEditorFactory::defaultFactory(); return factory->createEditor(t, parent);}/*! Sets the data to be displayed and edited by the \a editor for the item specified by \a index.*/void QItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{#ifdef QT_NO_PROPERTIES Q_UNUSED(editor); Q_UNUSED(index);#else Q_D(const QItemDelegate); QVariant v = index.data(Qt::EditRole); QByteArray n = d->editorFactory()->valuePropertyName(v.type()); if (!n.isEmpty()) editor->setProperty(n, v);#endif}/*! Sets the data for the specified \a model and item \a index from that supplied by the \a editor.*/void QItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{#ifdef QT_NO_PROPERTIES Q_UNUSED(editor); Q_UNUSED(model); Q_UNUSED(index);#else Q_D(const QItemDelegate); Q_ASSERT(model); Q_ASSERT(editor); QVariant::Type t = model->data(index, Qt::EditRole).type(); QByteArray n = d->editorFactory()->valuePropertyName(t); if (!n.isEmpty()) model->setData(index, editor->property(n), Qt::EditRole);#endif}/*! Updates the \a editor for the item specified by \a index according to the style \a option given.*/void QItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const{ if (editor) { Q_ASSERT(index.isValid()); QPixmap pixmap = decoration(option, index.data(Qt::DecorationRole)); QString text = index.data(Qt::EditRole).toString(); QRect pixmapRect = pixmap.rect(); QRect textRect(0, 0, editor->fontMetrics().width(text), editor->fontMetrics().lineSpacing()); QRect checkRect = check(option, textRect, index.data(Qt::CheckStateRole)); QStyleOptionViewItem opt = option; opt.showDecorationSelected = true; // let the editor take up all available space doLayout(opt, &checkRect, &pixmapRect, &textRect, false); editor->setGeometry(textRect); }}/*! Returns the editor factory used by the item delegate. If no editor factory is set, the function will return null. \sa setItemEditorFactory()*/QItemEditorFactory *QItemDelegate::itemEditorFactory() const{ Q_D(const QItemDelegate); return d->f;}/*! Sets the editor factory to be used by the item delegate to be the \a factory specified. If no editor factory is set, the item delegate will use the default editor factory. \sa itemEditorFactory()*/void QItemDelegate::setItemEditorFactory(QItemEditorFactory *factory){ Q_D(QItemDelegate); d->f = factory;}/*! Renders the item view \a text within the rectangle specified by \a rect using the given \a painter and style \a option.*/void QItemDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const{ QPen pen = painter->pen(); QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled; if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) cg = QPalette::Inactive; if (option.state & QStyle::State_Selected) { painter->fillRect(rect, option.palette.brush(cg, QPalette::Highlight)); painter->setPen(option.palette.color(cg, QPalette::HighlightedText)); } else { 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(); } QFont font = painter->font(); painter->setFont(option.font); QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding QString str = text; if (painter->fontMetrics().width(text) > textRect.width() && !text.contains(QLatin1Char('\n'))) str = elidedText(option.fontMetrics, textRect.width(), option.textElideMode, text); qt_format_text(option.font, textRect, option.displayAlignment, str, 0, 0, 0, 0, painter); painter->setFont(font); painter->setPen(pen);}/*! 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{ if (!pixmap.isNull() && !rect.isEmpty()) { QPoint p = QStyle::alignedRect(option.direction, option.decorationAlignment, pixmap.size(), rect).topLeft(); if (option.state & QStyle::State_Selected) { bool enabled = option.state & QStyle::State_Enabled; QPixmap *pm = selected(pixmap, option.palette, 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{ if (option.state & QStyle::State_HasFocus) { QStyleOptionFocusRect o; o.QStyleOption::operator=(option); o.rect = rect; o.state |= QStyle::State_KeyboardFocusChange; 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::Background); QApplication::style()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter); }}/*! 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{ 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; } QApplication::style()->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter);}/*! \internal*/void QItemDelegate::doLayout(const QStyleOptionViewItem &option, QRect *checkRect, QRect *pixmapRect, QRect *textRect, bool hint) const{ Q_ASSERT(checkRect && pixmapRect && textRect); int x = option.rect.left();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -