QT6 源(137)模型视图架构里的代理总基类 AbstractItemDelegate:

(1)给出本类的继承关系

在这里插入图片描述

(2)给出完整的源代码。不再举例,这是抽象类,不太好举例 :

#ifndef QABSTRACTITEMDELEGATE_H
#define QABSTRACTITEMDELEGATE_H

#include <QtWidgets/qtwidgetsglobal.h>
#include <QtCore/qobject.h>
#include <QtWidgets/qstyleoption.h>

QT_REQUIRE_CONFIG(itemviews);

QT_BEGIN_NAMESPACE

class QPainter;
class QModelIndex;
class QAbstractItemModel;
class QAbstractItemView;
class QHelpEvent;
class QAbstractItemDelegatePrivate;

/*
The QAbstractItemDelegate class is used to display and edit data items from a model.

Detailed Description:

A QAbstractltemDelegate 提供模型/视图架构中委托的接口和通用功能。
委托在视图中显示单个项目,并处理模型数据的编辑。
A QAbstractItemDelegate provides the interface and
    common functionality for delegates in the model/view architecture.
Delegates display individual items in views, and handle the editing of model data.

QAbstractltemDelegate类是模型/视图类之一,并且是Qt的模型/视图框架的一部分。
The QAbstractItemDelegate class is one of the Model/View Classes and
    is part of Qt's model/view framework.

要以自定义方式呈现项目,您必须实现 paint()和 sizeHint()。
QStyledltemDelegate类为这些函数提供了默认实现;如果您不需要自定义渲染,请改为对该类进行 Subclass。
To render an item in a custom way, you must implement paint() and sizeHint().
The QStyledItemDelegate class provides default implementations for these functions;
if you do not need custom rendering, subclass that class instead.

我们提供了一个在项目中绘制进度条的示例;在我们的案例中,一个包管理程序。
We give an example of drawing a progress bar in items;
in our case for a package management program.

图片略,无法展示。

我们在paint()函数中进行绘图:
我们创建了WidgetDelegate类,它继承自QStyledltemDelegate。
We create the WidgetDelegate class, which inherits from QStyledItemDelegate.
We do the drawing in the paint() function:

    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来绘制它。
Notice that we use a QStyleOptionProgressBar and initialize its members.
We can then use the current QStyle to draw it.

为了提供定制编辑功能,可以采用两种方法。
第一种方法是创建编辑组件并将其直接显示在项的上方。
要做到这一点,必须重写`createEditor()'方法以提供编辑组件,
使用`setEditorData ()'方法将数据从模型填充到编辑器中,
并使用`setModelData()、方法使代理能够根据编辑器中的数据更新模型。
To provide custom editing, there are two approaches that can be used.
The first approach is to create an editor widget and display it directly on top of the item.
To do this you must reimplement createEditor() to provide an editor widget,
    setEditorData() to populate the editor with the data from the model,
    and setModelData() so that the delegate can update the model with data from the editor.

第二种方法是通过重新实现editorEvent()直接处理用户事件。
The second approach is to handle user events directly by reimplementing editorEvent().

*/

class Q_WIDGETS_EXPORT QAbstractItemDelegate : public QObject
{
    Q_OBJECT

protected:
    QAbstractItemDelegate(QObjectPrivate &, QObject * parent = nullptr); //有参构造函数
private:
    Q_DECLARE_PRIVATE(QAbstractItemDelegate)
    Q_DISABLE_COPY(QAbstractItemDelegate)
    Q_PRIVATE_SLOT(d_func(), void _q_commitDataAndCloseEditor(QWidget*))

Q_SIGNALS:
    //This signal must be emitted when the editor widget has completed editing the data,
    //  and wants to write it back into the model.
    //当编辑器小部件完成数据编辑并希望将其写回模型时,必须发出此信号。
    void commitData (QWidget * editor); //在代理里完成对条目的修改,要提交修改时触发本信号

    //当用户使用指定的编辑器完成对项目进行编辑时,会触发此信号。
    //它向这些组件指示接这个提示为代理提供了一个途径,使其能够影响编辑完成后模型和视图的行为。
    //"下来应执行什么操作,以提供用户舒适的编辑体验。
    //例如,如果指定了“编辑下一个项目”,那么视图应使用一个代理来在模型中的下一个项目上打开编辑器。
    //This signal is emitted when the user has finished editing an item using the
    //  specified editor.
    //The hint provides a way for the delegate to influence how the
    //  model and view behave after editing is completed.
    //It indicates to these components what action should be performed next to provide a
    //  comfortable editing experience for the user.
    //For example, if EditNextItem is specified,
    //  the view should use a delegate to open an editor on the next item in the model.
    void closeEditor(QWidget * editor, QAbstractItemDelegate::EndEditHint hint = NoHint);

    void sizeHintChanged(const QModelIndex & index);
    //This signal must be emitted when the sizeHint() of index changed.
    //Views automatically connect to this signal and relayout items as necessary.

public:

    //这个枚举描述了委托可以向模型和视图组件提供的不同提示,从而使用户在模型中编辑数据的过程更加顺畅。
    //This enum describes the different hints that the delegate can give to the
    //  model and view components to make editing data in a model a
    //  comfortable experience for the user.
    enum EndEditHint {
        NoHint          ,   //There is no recommended action to be performed.

        //These hints let the delegate influence the behavior of the view:
        EditNextItem    ,   //The view should use the delegate to open an editor on the
                            //  next / previous item in the view.
        EditPreviousItem,   //视图应使用委托在视图中的下(上)一个项目上打开编辑器。

    //请注意,自定义视图可能对“下一个”和“上一个”的概念有不同的解释。
    //Note that custom views may interpret the concepts of next and previous differently.
    //当使用那些缓存数据的模型时,如那些为了提升性能或节省网络带宽而在本地处理数据的模型,以下提示最为有用。
    //The following hints are most useful when models are used that cache data,
    //  such as those that manipulate data locally in order to
    //  increase performance or conserve保存 network bandwidth.
        SubmitModelCache,   //If the model caches data,
                            //it should write out cached data to the underlying data store.
                            //如果模型缓存数据,它应该将缓存的数据写入底层数据存储。
        RevertModelCache    //If the model caches data,
                            //it should discard cached data and
                            //replace it with data from the underlying data store.
                            //如果模型缓存数据,它应该丢弃缓存的数据,并用底层数据存储中的数据替换。
    };
    //Although models and views should respond to these hints in appropriate ways,
    //  custom components may ignore any or all of them if they are not relevant.
    //尽管模型和视图应该以适当的方式响应这些提示,但如果它们不相关,自定义组件可能会忽略其中任何一个或全部。


    //Creates a new abstract item delegate with the given parent.
    explicit QAbstractItemDelegate(QObject * parent = nullptr); //有参构造函数

    virtual ~QAbstractItemDelegate(); //析构函数

    // painting
    virtual void      paint(      QPainter             * painter,
                            const QStyleOptionViewItem & option , //这个样式表,先不学
                            const QModelIndex          & index) const = 0;
    //This pure abstract function must be reimplemented
    //  if you want to provide custom rendering.
    //Use the painter and style option to render the item specified by the item index.
    //If you reimplement this you must also reimplement sizeHint().
    //用画家 patiner 和 样式 option来渲染索引 index 指向的条目。

    //This pure abstract function must be reimplemented
    //  if you want to provide custom rendering.
    //The options are specified by option and the model item by index.
    //If you reimplement this you must also reimplement paint(). 不懂形参是啥意思。
    virtual QSize     sizeHint( const QStyleOptionViewItem & option,
                                const QModelIndex          & index) const = 0;

    // editing
    virtual QWidget * createEditor(       QWidget              * parent,
                                    const QStyleOptionViewItem & option,
                                    const QModelIndex          & index) const;
    //Returns the editor to be used for editing the data item with the given index.
    //Note that the index contains information about the model being used.
    //The editor's parent widget is specified by parent, and the item options by option.
    //本函数返回代理里的组件,其父类是形参 parent,样式是 option,对应索引 index指向的条目。
    //The base implementation returns nullptr.
    //If you want custom editing you will need to reimplement this function.
    //返回的编辑器小部件应具有 Qt::StrongFocus 标志;
    //否则,小部件接收到的 QMouseEvents 将会被传递到视图。
    //除非编辑器自行绘制其背景(例如通过 setAutoFilBackground()方法),否则视图的背景将显现出来。
    //The returned editor widget should have Qt::StrongFocus;
    //otherwise, QMouseEvents received by the widget will propagate to the view.
    //The view's background will shine through unless the
    //  editor paints its own background (e.g., with setAutoFillBackground()).


    //从形参索引 index指向的模型条目里获取数据,显示到代理组件,即形参 1的 editor里。
    //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.
    virtual void      setEditorData(QWidget * editor, const QModelIndex & index) const;
    //Sets the contents of the given editor to the data for the item at the given index.
    //Note that the index contains information about the model being used.


    //将代理组件的数据保存到数据模型中。
    //从代理组件 editor里取出数据存入模型 model里索引 index指向的条目里。
    //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.
    virtual void      setModelData( QWidget            * editor,
                                    QAbstractItemModel * model ,
                                    const QModelIndex  & index) const;
    //Sets the data for the item at the given index in the model to the
    //  contents of the given editor.


    //设置组件大小,根据形参 option里给出的矩形大小,来设置index对应的代理editor的大小。
    //Updates the editor for the item specified by index according to the
    //  style option given.
    virtual void      updateEditorGeometry(       QWidget              * editor,
                                            const QStyleOptionViewItem & option,
                                            const QModelIndex          & index) const;
    //Updates the geometry of the editor for the item with the given index,
    //  according to the rectangle specified in the option.
    //If the item has an internal layout, the editor will be laid out accordingly.
    //Note that the index contains information about the model being used.
    //The base implementation does nothing.
    //If you want custom editing you must reimplement this function.

    //Called when the editor is no longer needed for editing the
    //  data item with the given index and should be destroyed.
    //The default behavior is a call to deleteLater on the editor.
    //It is possible e.g. to avoid this delete by reimplementing this function.
    //删除对应索引 index的代理组件 editor。
    virtual void      destroyEditor(QWidget * editor, const QModelIndex & index) const;

    // for non-widget editors
    virtual bool      editorEvent(          QEvent               * event,
                                            QAbstractItemModel   * model,
                                    const   QStyleOptionViewItem & option,
                                    const   QModelIndex          & index);
    //When editing of an item starts,
    //  this function is called with the event that triggered the editing,
    //the model, the index of the item, and the option used for rendering the item.
    //当对某个项目进行编辑时,此函数会被调用,
    //传递的参数包括触发编辑的事件、模型、项目的索引以及用于渲染项目的选项。
    //Mouse events are sent to editorEvent() even if they don't start editing of the item.
    //This can, for instance, be useful if you wish to open a context menu when the
    //  right mouse button is pressed on an item.
    //The base implementation returns false (indicating that it has not handled the event).
    //鼠标事件会被发送到editorEvent ()函数,即使这些事件并未触发对项目的编辑操作。
    //例如,这可能在右键点击某个项目时触发相应的上下文菜单时非常有用。
    //  基类实现返回false(表示它没有处理该事件)


    //每当发生帮助事件时,此函数会调用,并使用事件视图选项和与事件发生项对应的索引。
    //Whenever a help event occurs,
    //this function is called with the event view option and the
    //  index that corresponds to the item where the event occurs.
    //如果委托能够处理该事件,则返回 true;否则返回 false。
    //Returns true if the delegate can handle the event; otherwise returns false.
    //返回值为 true 表示使用该索引获取的数据具有所需的角色。
    //A return value of true indicates that the data obtained using the
    //  index had the required role.
    //对于已成功处理的QEvent:ToolTip和OEvent::WhatsThis事件,
    //根据用户的系统配置,可能会显示相关的弹出窗口。
    //For QEvent::ToolTip and QEvent::WhatsThis events that were handled successfully,
    //  the relevant popup may be shown depending on the user's system configuration.
    virtual bool        helpEvent(          QHelpEvent           * event,
                                            QAbstractItemView    * view,
                                    const   QStyleOptionViewItem & option,
                                    const   QModelIndex          & index);


    virtual QList<int>  paintingRoles() const; //无官方注释。


}; //class QAbstractItemDelegate : public QObject

QT_END_NAMESPACE

#endif // QABSTRACTITEMDELEGATE_H

(3)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangzhangkeji

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值