📄 qitemdelegate.cpp
字号:
QString QItemDelegatePrivate::valueToText(const QVariant &value, const QStyleOptionViewItemV3 &option){ QString text; switch (value.type()) { case QVariant::Double: text = option.locale.toString(value.toDouble()); break; case QVariant::Int: case QVariant::LongLong: text = option.locale.toString(value.toLongLong()); break; case QVariant::UInt: case QVariant::ULongLong: text = option.locale.toString(value.toULongLong()); break; case QVariant::Date: text = option.locale.toString(value.toDate(), QLocale::ShortFormat); break; case QVariant::Time: text = option.locale.toString(value.toTime(), QLocale::ShortFormat); break; case QVariant::DateTime: text = option.locale.toString(value.toDateTime().date(), QLocale::ShortFormat); text += QLatin1Char(' '); text += option.locale.toString(value.toDateTime().time(), QLocale::ShortFormat); break; default: text = replaceNewLine(value.toString()); break; } return text;}/*! Renders the delegate using the given \a painter and style \a option for the item specified by \a index. When reimplementing this function in a subclass, you should update the area held by the option's \l{QStyleOption::rect}{rect} variable, using the option's \l{QStyleOption::state}{state} variable to determine the state of the item to be displayed, and adjust the way it is painted accordingly. For example, a selected item may need to be displayed differently to unselected items, as shown in the following code: \quotefromfile itemviews/pixelator/pixeldelegate.cpp \skipto QStyle::State_Selected \printuntil else \dots After painting, you should ensure that the painter is returned to its the state it was supplied in when this function was called. For example, it may be useful to call QPainter::save() before painting and QPainter::restore() afterwards. \sa QStyle::State*/void QItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ Q_D(const QItemDelegate); Q_ASSERT(index.isValid()); QStyleOptionViewItemV3 opt = setOptions(index, option); const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(&option); opt.features = v2 ? v2->features : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None); const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option); opt.locale = v3 ? v3->locale : QLocale(); opt.widget = v3 ? v3->widget : 0; // prepare painter->save(); if (d->clipPainting) painter->setClipRect(opt.rect); // get the data and the rectangles QVariant value; QPixmap pixmap; QRect decorationRect; value = index.data(Qt::DecorationRole); if (value.isValid()) { // ### we need the pixmap to call the virtual function pixmap = decoration(opt, value); if (value.type() == QVariant::Icon) { d->tmp.icon = qvariant_cast<QIcon>(value); d->tmp.mode = d->iconMode(option.state); d->tmp.state = d->iconState(option.state); const QSize size = d->tmp.icon.actualSize(option.decorationSize, d->tmp.mode, d->tmp.state); decorationRect = QRect(QPoint(0, 0), size); } else { d->tmp.icon = QIcon(); decorationRect = QRect(QPoint(0, 0), pixmap.size()); } } else { d->tmp.icon = QIcon(); decorationRect = QRect(); } QString text; QRect displayRect; value = index.data(Qt::DisplayRole); if (value.isValid()) { text = QItemDelegatePrivate::valueToText(value, opt); displayRect = textRectangle(painter, d->textLayoutBounds(opt), opt.font, text); } QRect checkRect; Qt::CheckState checkState = Qt::Unchecked; value = index.data(Qt::CheckStateRole); if (value.isValid()) { checkState = static_cast<Qt::CheckState>(value.toInt()); checkRect = check(opt, opt.rect, value); } // do the layout doLayout(opt, &checkRect, &decorationRect, &displayRect, false); // draw the item drawBackground(painter, opt, index); drawCheck(painter, opt, checkRect, checkState); drawDecoration(painter, opt, decorationRect, pixmap); drawDisplay(painter, opt, displayRect, text); drawFocus(painter, opt, displayRect); // done painter->restore();}/*! Returns the size needed by the delegate to display the item specified by \a index, taking into account the style information provided by \a option. When reimplementing this function, note that in case of text items, QItemDelegate adds a margin (i.e. 2 * QStyle::PM_FocusFrameHMargin) to the length of the text.*/QSize QItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const{ QVariant value = index.data(Qt::SizeHintRole); if (value.isValid()) return qvariant_cast<QSize>(value); QRect decorationRect = rect(option, index, Qt::DecorationRole); QRect displayRect = rect(option, index, Qt::DisplayRole); QRect checkRect = rect(option, index, Qt::CheckStateRole); doLayout(option, &checkRect, &decorationRect, &displayRect, true); return (decorationRect|displayRect|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 = static_cast<QVariant::Type>(index.data(Qt::EditRole).userType()); 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. The default implementation uses the editor's user property to set values. \sa QMetaProperty::isUser()*/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 = editor->metaObject()->userProperty().name(); // ### Qt 5: remove // A work-around for missing "USER true" in qdatetimeedit.h for // QTimeEdit's time property and QDateEdit's date property. // It only triggers if the default user property "dateTime" is // reported for QTimeEdit and QDateEdit. if (n == "dateTime") { if (editor->inherits("QTimeEdit")) n = "time"; else if (editor->inherits("QDateEdit")) n = "date"; } if (n.isEmpty()) n = d->editorFactory()->valuePropertyName(static_cast<QVariant::Type>(v.userType())); 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. The default implementation uses the editor's user property to get values. \sa QMetaProperty::isUser()*/void QItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{#ifdef QT_NO_PROPERTIES Q_UNUSED(model); Q_UNUSED(editor); Q_UNUSED(index);#else Q_D(const QItemDelegate); Q_ASSERT(model); Q_ASSERT(editor); QByteArray n = editor->metaObject()->userProperty().name(); if (n.isEmpty()) n = d->editorFactory()->valuePropertyName( static_cast<QVariant::Type>(model->data(index, Qt::EditRole).userType())); 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) return; Q_ASSERT(index.isValid()); QPixmap pixmap = decoration(option, index.data(Qt::DecorationRole)); QString text = QItemDelegatePrivate::replaceNewLine(index.data(Qt::DisplayRole).toString()); QRect pixmapRect = QRect(QPoint(0, 0), option.decorationSize).intersected(pixmap.rect()); QRect textRect = textRectangle(0, option.rect, option.font, text); 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{ Q_D(const QItemDelegate); if (text.isEmpty()) return; 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 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -