QStyledItemDelegate Class

QStyledItemDelegate 为模型中的数据项提供显示和编辑功能. 更多...

头文件: #include <QStyledItemDelegate>
qmake: QT += widgets
开始支持版本: Qt 4.4
基类: QAbstractItemDelegate

公有函数

QStyledItemDelegate(QObject *parent = nullptr)
virtual ~QStyledItemDelegate()
virtual QString displayText(const QVariant &value, const QLocale &locale) const
QItemEditorFactory *itemEditorFactory() const
void setItemEditorFactory(QItemEditorFactory *factory)

重新实现的公有函数

virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override

受保护的函数

virtual void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const

重新实现的受保护函数

virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
virtual bool eventFilter(QObject *editor, QEvent *event) override
  • 9 个受保护的函数继承自 QObject

其他继承的成员

详细描述

在 Qt 项视图中显示来自模型的数据时, 各个项都是由委托绘制的, 如., QTableView. 当编辑一个项目时, 它提供一个编辑器部件, 它被放置在项目视图的顶部. QStyledItemDelegate 是所有Qt项视图的默认委托, 在创建时安装在项目上面.

The QStyledItemDelegate 是 模型/视图类 之一, 是 Qt 的 模型/视图框架的一部分. 委托允许项目显示和编辑独立于模型和视图开发.

对模型中每一项数据分配一个 ItemDataRole; 每个项可以存储一个 QVariant 角色. QStyledItemDelegate 实现了用户期望的常见数据类型显示和编辑, 包括布尔值, 整数和字符串.

它们在模型中的角色不同, 数据的绘制方式将有所不同. 下表描述了角色和委托人可以为每个角色提供的数据处理类型. 要确保模型为每个角色返回适当的数据, 来确定视图中每一项的外观.

使用 QItemEditorFactory 创建编辑器; QItemEditorFactory 提供的默认静态实例安装在所有项目委托上. 你可以使用 setItemEditorFactory() 设置自定义工厂, 或者使用 QItemEditorFactory::setDefaultFactory() 设置新的默认工厂. 被编辑的是存储在带有 EditRole 的项目模型中的数据. 有关项目编辑器工厂的更高级介绍, 参见 QItemEditorFactory. The Color Editor Factory 示例展示了如何使用工厂创建自定义编辑器.

子类化 QStyledItemDelegate

如果委托不支持绘制你需要的数据类型, 或者你想定制项目的绘制, 你需要子类化 QStyledItemDelegate, 并重新实现 paint() 和 sizeHint(). paint() 函数是为每个项单独调用, 使用 sizeHint() 可以为每个项指定提示.

在重新实现 paint() 时, 通常会处理自己想要绘制的数据类型, 并对其他类型使用超类实现.

复选框指示器的绘制是由当前的样式来执行的. 样式还指定了不同数据角色的大小和绘制数据的边界矩形. 项目本身的边界矩形也由样式计算. 因此, 当绘制已经支持的数据类型时, 最好询问这些边框的样式. 最好向样式询问这些边界矩形. 详见 QStyle.

如果你想改变任何一个由样式计算出的边界矩形或复选框指示器, 你可以子类 QStyle. 注意, 重新实现 sizeHint(), 会影响项的大小.

自定义委托可以在不使用编辑器项工厂的情况下提供编辑器. 在这种情况下, 必须重新实现以下虚函数:

Star Delegate 示例通过重新实现这些方法来创建编辑器.

QStyledItemDelegate vs. QItemDelegate

从 Qt 4.4 开始, 有两个委托类: QItemDelegate 和 QStyledItemDelegate. 但是, 默认委托是 QStyledItemDelegate. 这两个类是绘画和为视图中的项目提供编辑器的独立替代方案. 它们之间的区别在于 QStyledItemDelegate 使用当前样式来绘制其项目. 因此, 我们建议在实现自定义委托或使用 Qt 样式表时, 使用 QStyledItemDelegate 作为基类. 除非自定义委托需要使用该样式进行绘图, 否则任一类所需的代码应该相同.

如果你希望自定义项目视图的绘制, 则应该实现自定义样式. 详见 QStyle.

另见 Delegate Classes, QItemDelegate, QAbstractItemDelegate, QStyle, Spin Box Delegate Example, Star Delegate Example, Color Editor Factory Example.

成员函数

QStyledItemDelegate::QStyledItemDelegate(QObject *parent = nullptr)

Constructs an item delegate with the given parent.

[virtual] QStyledItemDelegate::~QStyledItemDelegate()

Destroys the item delegate.

[override virtual] QWidget *QStyledItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const

重新实现: QAbstractItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const.

Returns the widget used to edit the item specified by index for editing. The parent widget and style option are used to control how the editor widget appears.

另见 QAbstractItemDelegate::createEditor().

[virtual] QString QStyledItemDelegate::displayText(const QVariant &value, const QLocale &locale) const

This function returns the string that the delegate will use to display the Qt::DisplayRole of the model in locale. value is the value of the Qt::DisplayRole provided by the model.

The default implementation uses the QLocale::toString to convert value into a QString.

This function is not called for empty model indices, i.e., indices for which the model returns an invalid QVariant.

另见 QAbstractItemModel::data().

[override virtual protected] bool QStyledItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)

重新实现: QAbstractItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index).

[override virtual protected] bool QStyledItemDelegate::eventFilter(QObject *editor, QEvent *event)

重新实现: QObject::eventFilter(QObject *watched, QEvent *event).

Returns true if the given editor is a valid QWidget and the given event is handled; otherwise returns false. The following key press events are handled by default:

  • Tab
  • Backtab
  • Enter
  • Return
  • Esc

If the editor's type is QTextEdit or QPlainTextEdit then Enter and Return keys are not handled.

In the case of Tab, Backtab, Enter and Return key press events, the editor's data is committed to the model and the editor is closed. If the event is a Tab key press the view will open an editor on the next item in the view. Likewise, if the event is a Backtab key press the view will open an editor on the previous item in the view.

If the event is a Esc key press event, the editor is closed without committing its data.

另见 commitData() and closeEditor().

[virtual protected] void QStyledItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const

Initialize option with the values using the index index. This method is useful for subclasses when they need a QStyleOptionViewItem, but don't want to fill in all the information themselves.

另见 QStyleOption::initFrom().

QItemEditorFactory *QStyledItemDelegate::itemEditorFactory() const

Returns the editor factory used by the item delegate. If no editor factory is set, the function will return null.

另见 setItemEditorFactory().

[override virtual] void QStyledItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const

重新实现: QAbstractItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const.

Renders the delegate using the given painter and style option for the item specified by index.

This function paints the item using the view's QStyle.

When reimplementing paint in a subclass. Use the initStyleOption() to set up the option in the same way as the QStyledItemDelegate.

Whenever possible, use the option while painting. Especially its rect variable to decide where to draw and its state to determine if it is enabled or selected.

After painting, you should ensure that the painter is returned to 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.

另见 QItemDelegate::paint(), QStyle::drawControl(), and QStyle::CE_ItemViewItem.

[override virtual] void QStyledItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const

重新实现: QAbstractItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const.

Sets the data to be displayed and edited by the editor from the data model item specified by the model index.

The default implementation stores the data in the editor widget's user property.

另见 QMetaProperty::isUser().

void QStyledItemDelegate::setItemEditorFactory(QItemEditorFactory *factory)

Sets the editor factory to be used by the item delegate to be the factory specified. If no editor factory is set, the item delegate will use the default editor factory.

另见 itemEditorFactory().

[override virtual] void QStyledItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const

重新实现: QAbstractItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const.

Gets data from the editor widget and stores it in the specified model at the item index.

The default implementation gets the value to be stored in the data model from the editor widget's user property.

另见 QMetaProperty::isUser().

[override virtual] QSize QStyledItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const

重新实现: QAbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const.

Returns the size needed by the delegate to display the item specified by index, taking into account the style information provided by option.

This function uses the view's QStyle to determine the size of the item.

另见 QStyle::sizeFromContents() and QStyle::CT_ItemViewItem.

[override virtual] void QStyledItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const

重新实现: QAbstractItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const.

Updates the editor for the item specified by index according to the style option given.