QAbstractItemDelegate类参考
QAbstractItemDelegate类用于显示和编辑来自模型中的数据项。更多
#include< QAbstractItemDelegate>
继承自:QObject。
被继承:QItemDelegate and QStyledItemDelegate.
公共类型
enum | EndEditHint { NoHint, EditNextItem, EditPreviousItem, SubmitModelCache, RevertModelCache } |
公有函数
QAbstractItemDelegate(QObject * parent = 0) | |
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 |
- 31 个继承自 QObject的公有函数
信号
-
void closeEditor(QWidget * editor, QAbstractItemDelegate::EndEditHint hint = NoHint) void commitData(QWidget * editor) void sizeHintChanged(const QModelIndex & index)
2个继承自QObject的信号。
额外的继承成员 - 1个属性继承自QObject
- 1个槽继承自QObject
- 1个公有变量继承自QObject
- 10个静态公共成员继承自QObject
- 9个保护函数继承自QObject
- 2个保护变量继承自QObject
详细说明
QAbstractItemDelegate类用于显示和编辑来自模型的数据项。
QAbstractItemDelegate为模型/视图架构中的代理提供了一个接口和公共功能。代理显示视图中的项目,并处理模型数据的编辑。
QAbstractItemDelegate类是模型/视图类中的一个,是模型/视图架构的一部分。
为了以一个自定义的方式渲染一个项,您必须实现paint()和sizehint()。QItemDelegate类提供了这些功能的默认实现;如果你不需要自定义绘制,子类化QItemDelegate即可。
我们给出一个在项目中绘制进度栏的例子;在我们的案例中是一个软件包管理程序。
我们创建了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()。
参见 Model/View Programming, QItemDelegate, Pixelator Example, QStyledItemDelegate, and QStyle
成员类型文档
enum QAbstractItemDelegate::EndEditHint
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.
Constant | Value | Description |
---|---|---|
QAbstractItemDelegate::NoHint | 0 | There is no recommended action to be performed. |
These hints let the delegate influence the behavior of the view:
Constant | Value | Description |
---|---|---|
QAbstractItemDelegate::EditNextItem | 1 | The view should use the delegate to open an editor on the next item in the view. |
QAbstractItemDelegate::EditPreviousItem | 2 | The view should use the delegate to open an editor on the previous item in the view. |
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.
Constant | Value | Description |
---|---|---|
QAbstractItemDelegate::SubmitModelCache | 3 | If the model caches data, it should write out cached data to the underlying data store. |
QAbstractItemDelegate::RevertModelCache | 4 | 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.
成员函数文档
QAbstractItemDelegate::QAbstractItemDelegate(QObject * parent = 0)
用给定的父类创建一个新的抽象项代理。。
QAbstractItemDelegate::~QAbstractItemDelegate() [virtual]
销毁抽象项代理
void QAbstractItemDelegate::closeEditor(QWidget * editor,QAbstractItemDelegate::EndEditHint hint = NoHint) [signal]
当用户用指定的编辑器完成了一个项的编辑时此信号被发出。
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.
See also EndEditHint.
void QAbstractItemDelegate::commitData(QWidget * editor) [signal]
当编辑控件已经完成数据的编辑,并且想要把它写回模型时,此信号必须被发出。
QWidget * QAbstractItemDelegate::createEditor(QWidget * parent, constQStyleOptionViewItem & option, const QModelIndex & index) const [virtual]
注意该索引包含了模型正使用的信息 编辑器的父部件被 parent指定, item选项被 option指定.
基类实例返回0. 如果你想要自定义编辑器,你需要重载此函数。
返回的编辑器部件应该有 Qt::StrongFocus; 否则, 被部件接收的 QMouseEvent 将投递给视图 。视图的背景将闪烁除非通过编辑器绘制它自己的背景 (例如用 setAutoFillBackground()).
See also destroyEditor(), setModelData(), and setEditorData().
void QAbstractItemDelegate::destroyEditor(QWidget * editor, constQModelIndex & index) const [virtual]
默认的行为是在编辑器删除后调用一次。通过重载此函数可避免删除!
这个函数在 Qt 5.0中被引进.
也可查看 createEditor().
bool QAbstractItemDelegate::editorEvent(QEvent * event, QAbstractItemModel* model, const QStyleOptionViewItem & option, const QModelIndex & index)[virtual]
当一个项开始编辑时, 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).
bool QAbstractItemDelegate::helpEvent(QHelpEvent * event, QAbstractItemView* view, const QStyleOptionViewItem & option, const QModelIndex & index)[virtual]
当一个帮助事件发生时,此函数被调用 with the event view option and the index that corresponds to the item where the event occurs.
Returns true if the delegate can handle the event; otherwise returns false. A return value of true indicates that the data obtained using the index had the required role.
For QEvent::ToolTip and QEvent::WhatsThis events that were handled successfully, the relevant popup may be shown depending on the user's system configuration.
This function was introduced in Qt 4.3.
See also QHelpEvent.
void QAbstractItemDelegate::paint(QPainter * painter, constQStyleOptionViewItem & option, const QModelIndex & index) const [pure virtual]
Use thepainter and style option to render the item specified by the item index.
If you reimplement this you must also reimplement sizeHint().
void QAbstractItemDelegate::setEditorData(QWidget * editor, constQModelIndex & index) const [virtual]
在给定索引项处设置给定的编辑器的内容。
注意索引包含了正在被使用模型的相关信息。
基类实例没有做任何事情. 如果你想要自定义编辑,你需要重载此函数。
也可查看 setModelData().
void QAbstractItemDelegate::setModelData(QWidget * editor,QAbstractItemModel * model, const QModelIndex & index) const [virtual]
基类实例没有做任何事情. 如果你想要自定义编辑,你需要重载此函数。
也可查看 setEditorData().
QSize QAbstractItemDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const [pure virtual]
The options are specified by option and the model item by index.
如果你重载了这个你也必须重载 paint().
void QAbstractItemDelegate::sizeHintChanged(const QModelIndex & index)[signal]
这个信号在索引的sizeHint()改变时必须被发射。
Views automatically connect to this signal and relayout items as necessary.
This function was introduced in Qt 4.4.
void QAbstractItemDelegate::updateEditorGeometry(QWidget * editor, constQStyleOptionViewItem & option, const QModelIndex & index) const [virtual]
用给定索引的项更新编辑器的几何形状 , 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.
基类实例没有做任何事情. 如果你想要自定义编辑,你需要重载此函数。