QAbstractItemDelegate Class

QAbstractItemDelegate 类用于显示和编辑模型中的数据项。更多

Header: #include <QAbstractItemDelegate>
CMake.QAbstractItemDelegate 类 find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QObject
继承于

QItemDelegateQStyledItemDelegate

公共类型

enum EndEditHint { NoHint, EditNextItem, EditPreviousItem, SubmitModelCache, RevertModelCache }

公共函数

QAbstractItemDelegate(QObject *parent = nullptr)
virtual ~QAbstractItemDelegate()
virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
virtual void destroyEditor(QWidget *editor, const QModelIndex &index) const
virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
virtual bool helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const = 0
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const = 0
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const

信号

void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint = NoHint)
void commitData(QWidget *editor)
void sizeHintChanged(const QModelIndex &index)

详细说明

QAbstractItemDelegate 为模型/视图架构中的委托提供接口和通用功能。代理在视图中显示单个项目,并处理模型数据的编辑。

QAbstractItemDelegate 类是模型/视图类之一,也是 Qt模型/视图框架的一部分。

要以自定义方式呈现项目,必须实现paint() 和sizeHint() 。QStyledItemDelegate 类提供了这些函数的默认实现;如果不需要自定义渲染,请子类该类。

我们将举例说明在项目中绘制进度条的情况;在我们的例子中,进度条是为软件包管理程序绘制的。

我们创建WidgetDelegate 类,该类继承于QStyledItemDelegate 。我们在paint() 函数中进行绘制:

void WidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const
{
    if (index.column() == 1) {
        int progress = index.data().toInt();

        QStyleOptionProgressBar progressBarOption;
        progressBarOption.rect = option.rect;
        progressBarOption.minimum = 0;
        progressBarOption.maximum = 100;
        progressBarOption.progress = progress;
        progressBarOption.text = QString::number(progress) + "%";
        progressBarOption.textVisible = true;

        QApplication::style()->drawControl(QStyle::CE_ProgressBar,
                                           &progressBarOption, painter);
    } else
        QStyledItemDelegate::paint(painter, option, index);

请注意,我们使用了QStyleOptionProgressBar 并初始化了其成员。然后,我们可以使用当前的QStyle 进行绘制。

要提供自定义编辑功能,可以使用两种方法。第一种方法是创建一个编辑器部件,并直接显示在项目的顶部。为此,您必须重新实现createEditor() 以提供一个编辑器部件、setEditorData() 以用模型中的数据填充编辑器,以及setModelData() 以使委托能用编辑器中的数据更新模型。

第二种方法是通过重新实现editorEvent() 来直接处理用户事件。

另请参阅 模型/视图编程QStyledItemDelegateQStyle

成员类型文档

enum QAbstractItemDelegate::EndEditHint

该枚举描述了委托可以向模型和视图组件提供的不同提示,以便让用户在模型中编辑数据时获得舒适的体验。

常量说明
QAbstractItemDelegate::NoHint0没有建议执行的操作。

这些提示可让委托人影响视图的行为:

常量说明
QAbstractItemDelegate::EditNextItem1视图应使用委托打开视图中下一个项目的编辑器。
QAbstractItemDelegate::EditPreviousItem2视图应使用委托打开视图中上一个项目的编辑器。

请注意,自定义视图可能会以不同的方式解释下一个和上一个的概念。

以下提示在使用缓存数据的模型(如为提高性能或节省网络带宽而在本地处理数据的模型)时最有用。

常数说明
QAbstractItemDelegate::SubmitModelCache3如果模型缓存数据,它应将缓存数据写出到底层数据存储。
QAbstractItemDelegate::RevertModelCache4如果模型缓存数据,则应丢弃缓存数据并用底层数据存储中的数据替换。

虽然模型和视图应该以适当的方式响应这些提示,但如果它们不相关,自定义组件可以忽略任何或所有提示。

成员函数文档

[explicit] QAbstractItemDelegate::QAbstractItemDelegate(QObject *parent = nullptr)

使用给定的parent 创建一个新的抽象项目委托。

[virtual noexcept] QAbstractItemDelegate::~QAbstractItemDelegate()

销毁抽象项目委托。

[signal] void QAbstractItemDelegate::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint = NoHint)

当用户使用指定的editor 完成对项目的编辑时,就会发出该信号。

hint 为委托提供了一种方法,使其能够影响模型和视图在编辑完成后的行为。它指示这些组件下一步应执行什么操作,以便为用户提供舒适的编辑体验。例如,如果指定了EditNextItem ,视图应使用委托打开模型中下一个项目的编辑器。

另请参阅 EndEditHint

[signal] void QAbstractItemDelegate::commitData(QWidget *editor)

editor widget 完成数据编辑并希望将其写回模型时,必须发出该信号。

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

返回用于编辑数据项的编辑器,其给定值为index 。请注意,索引包含所使用模型的相关信息。编辑器的父部件由parent 指定,项目选项由option 指定。

基本实现返回nullptr 。如果需要自定义编辑,则需要重新实现此函数。

返回的编辑器部件应具有Qt::StrongFocus ;否则,部件收到的QMouseEvent将传播到视图。除非编辑器绘制了自己的背景(例如,使用setAutoFillBackground()) ,否则视图的背景将被照亮。

另请参阅 destroyEditor()、setModelData() 和setEditorData()。

[virtual] void QAbstractItemDelegate::destroyEditor(QWidget *editor, const QModelIndex &index) const

editor 不再需要编辑带有给定index 的数据项并应销毁时调用。默认行为是在编辑器上调用 deleteLater。例如,可以通过重新实现此函数来避免删除。

另请参见 createEditor()。

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

开始编辑项目时,该函数会调用触发编辑的eventmodel 、项目的index 以及用于渲染项目的option

即使没有开始编辑项目,鼠标事件也会被发送到 editorEvent()。例如,如果您希望在按下项目上的鼠标右键时打开上下文菜单,这将非常有用。

基本实现返回false (表示未处理该事件)。

[virtual] bool QAbstractItemDelegate::helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)

每当发生帮助事件时,就会调用该函数,同时调用event view option 和与发生事件的项目相对应的index

如果委托可以处理该事件,则返回true ;否则返回false 。返回值为 true 表示使用索引获取的数据具有所需的角色。

对于成功处理的QEvent::ToolTipQEvent::WhatsThis 事件,根据用户的系统配置,可能会显示相关的弹出窗口。

另请参见 QHelpEvent

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

如果要提供自定义渲染,必须重新实现这个纯抽象函数。使用painter 和样式option 渲染项目index 所指定的项目。

如果要重新实现该功能,还必须重新实现sizeHint()。

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

将给定editor 的内容设置为给定index 的项目数据。请注意,索引中包含了所使用模型的相关信息。

基本实现不做任何操作。如果需要自定义编辑,则需要重新实现此函数。

另请参阅 setModelData() 。

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

model 中给定index 的项目数据设置为给定editor 的内容。

基本实现不做任何操作。如果需要自定义编辑,则需要重新实现此函数。

另请参阅 setEditorData().

[pure virtual] QSize QAbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const

如果要提供自定义渲染,必须重新实现这个纯抽象函数。选项由option 指定,模型项由index 指定。

如果重新实现该函数,还必须重新实现paint()。

[signal] void QAbstractItemDelegate::sizeHintChanged(const QModelIndex &index)

indexsizeHint() 发生变化时,必须发出该信号。

视图会自动连接到该信号,并根据需要中继输出项目。

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

index根据option 中指定的矩形更新editor 的几何图形。如果项目有内部布局,编辑器也会相应布局。请注意,索引包含所使用模型的相关信息。

基本实现不做任何操作。如果需要自定义编辑,则必须重新实现此函数。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.