(1)使用自定义代理时,不必理会析构视图的原有代理及理由。首先 视图可以没有选择模型,但视图是有默认代理的 :

++

(2)给出自定义代理的举例 :

++测试结果如下 :

(3)本样式代理的源代码定义于头文件 qstyleditemdelegate . h :
#ifndef QSTYLEDITEMDELEGATE_H
#define QSTYLEDITEMDELEGATE_H
#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qabstractitemdelegate.h>
#include <QtCore/qstring.h>
#include <QtGui/qpixmap.h>
#include <QtCore/qvariant.h>
QT_REQUIRE_CONFIG(itemviews);
QT_BEGIN_NAMESPACE
class QStyledItemDelegatePrivate;
class QItemEditorFactory;
/*
The QStyledItemDelegate class provides display and editing facilities for
data items from a model.
Detailed Description : 本代理是模型视图架构里的默认代理。
当在 Qt项目视图(例如 QTableView)中显示来自模型的数据时,各个项目由代理绘制。
此外,在编辑项目时,它会提供一个编辑器 Widget,该 Widget 在进行编辑时放置在项目视图的顶部。
QStyledltemDelegate 是所有 Qt 项目视图的默认代理,并在创建它们时安装在它们上。
When displaying data from models in Qt item views, e.g., a QTableView,
the individual items are drawn by a delegate.
Also, when an item is edited, it provides an editor widget,
which is placed on top of the item view while editing takes place.
QStyledItemDelegate is the default delegate for all Qt item views,
and is installed upon them when they are created.
QStyledltemDelegate类是模型/视图类之一,是Qt的模型/视图框架的一部分。
该代理允许独立于模型和视图来开发项的显示和编辑功能。
The QStyledItemDelegate class is one of the Model/View Classes and
is part of Qt's model/view framework.
The delegate allows the display and editing of items to
be developed independently from the model and view.
模型中的项目数据被分配了“项目数据角色”;每个项目可以存储每种角色对应的 QVariant。
QStyledItemDelegate 实现了对用户期望的最常见数据类型的显示和编辑功能,包括布尔值、整数和字符串。
这些数据会根据他们在模型中所扮演的角色以不同的方式提取。
下表描述了角色以及代表者能够处理的每种角色的数据类型。
通常只需确保模型为每种角色返回适当的数据,以确定项目在视图中的呈现方式就足够了
编辑器是通过 QItemEditorfactory创建的;QItemEditorFactory提供的默认静态实例被安装在所有项委托上。
您可以使用 setItemEditorFactory() 函数设置自定义工厂,
或者使用 QItemEditorFactory::setDefaultFactory(函数设置新的默认工厂。
被编辑的是与 EditRole相关联的存储在项模型中的数据。
关于项编辑器工厂的更高级介绍请参阅 QItemEditorFactory类。
Color Editor Factory示例展示了如何通过工厂创建自定义编辑器。
Subclassing QStyledItemDelegate :
如果委托不支持绘制所需的数据类型,或者您想要自定义项目的绘制,
则需要子类化QStyledItemDelegate,并重新实现paint()和可能的sizeHint()。
paint()函数是针对每个项单独调用的,使用sizeHint(),您可以为每个项指定提示。
在重新实现paint()方法时,通常会处理想要绘制的数据类型,并使用超类的实现来处理其他类型的对象。
复选框指示符的绘制由当前样式负责。该样式还指定用于绘制不同数据角色的数据的尺寸和边界矩形。
此外,项目本身的边界矩形也是由样式计算得出的。
因此,在绘制已支持的数据类型时,最好向样式请求这些边界矩形。QStyle类的描述对此有更为详细的说明。
如果您希望更改由样式或复选框指示符的绘制所计算出的任何边界矩形,可以子类化QStyle。
但请注意,项的大小也可能受到重新实现sizeHint()函数的影响。
自定义委托有可能在不使用编辑器项工厂的情况下提供编辑器。这种情况下,必须重新实现以下虚函数:
createEditor() 返回用于从模型更改数据的控件,并且可以重写以自定义编辑行为。
setEditorData()为小部件提供数据以进行操作。
updateEditorGeometry()确保编辑器与项目视图正确显示。
setModelData()将更新的数据返回给模型。
Star Delegate 示例通过重新实现这些方法来创建编辑器。
QStyledItemDelegate vs. QItemDelegate :
自Qt4.4起,有两个委托类:QItemDelegate和QStyledItemDelegate。
然而,默认的委托是QStyledItemDelegate。这两个类是独立的选项,用于绘制和为视图中的项提供编辑器。
它们之间的区别在于,QStyledItemDelegate使用当前样式来绘制其项。
因此,我们建议在实现自定义委托或处理Qt样式表时,将QStyledltemDelegate作为基类使用。
除非自定义委托需要使用样式进行绘图,否则这两个类所需的代码应该是相同的。
如果你想自定义项目视图的绘画效果,你应该实现一个自定义样式。请参阅QStyle类文档以了解详细信息。
*/
class Q_WIDGETS_EXPORT QStyledItemDelegate : public QAbstractItemDelegate
{
Q_OBJECT
protected:
//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.
//用 index里的数据填充到形参1 option里。
virtual void initStyleOption(QStyleOptionViewItem * option,
const QModelIndex & index) const;
bool eventFilter(QObject * object, QEvent * event) override;
bool editorEvent(QEvent * event, QAbstractItemModel * model,
const QStyleOptionViewItem & option,
const QModelIndex & index) override;
private:
Q_DECLARE_PRIVATE(QStyledItemDelegate)
Q_DISABLE_COPY(QStyledItemDelegate)
public:
//Constructs an item delegate with the given parent.
explicit QStyledItemDelegate(QObject * parent = nullptr); //构造函数
~QStyledItemDelegate();
// painting
void paint(QPainter * painter, const QStyleOptionViewItem & option,
const QModelIndex & index) const override;
QSize sizeHint(const QStyleOptionViewItem & option,
const QModelIndex & index) const override;
// editing
QWidget *createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const override;
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
// editor factory
QItemEditorFactory * itemEditorFactory() const;
void setItemEditorFactory(QItemEditorFactory * factory);
virtual QString 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.
}; //完结 class QStyledItemDelegate : public QAbstractItemDelegate
QT_END_NAMESPACE
#endif // QSTYLEDITEMDELEGATE_H
(4)
谢谢
2764

被折叠的 条评论
为什么被折叠?



