The 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 bool | editorEvent (QEvent * event , QAbstractItemModel * model , 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 |
| bool | helpEvent (QHelpEvent * event , QAbstractItemView * view , const QStyleOptionViewItem & option , const QModelIndex & index ) |
| void | closeEditor (QWidget * editor , QAbstractItemDelegate::EndEditHint hint = NoHint) |
| void | commitData (QWidget * editor ) |
| void | sizeHintChanged (const QModelIndex & index ) |
The QAbstractItemDelegate 类用于显示和编辑来自模型的数据项。
A QAbstractItemDelegate 为模型/视图体系结构中的委托,提供接口和常见功能。委托在视图中显示单个项,并处理模型数据的编辑。
The QAbstractItemDelegate 类是一种 模型/视图类 且属于 Qt 的 模型/视图框架 .
要以自定义方式渲染项,必须实现 paint () 和 sizeHint ()。 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 来绘制它。
要提供自定义编辑,有 2 种方式可以使用。第 1 种方式是创建编辑器 Widget 并将其直接显示在项顶部。要做到这点,必须重实现 createEditor () 以提供编辑器 Widget, setEditorData () 以采用来自模型的数据填充编辑器,和 setModelData () 以便委托可以采用来自编辑器的数据更新模型。
第 2 种方式是直接处理用户事件通过重实现 editorEvent ().
另请参阅 模型/视图编程 , QItemDelegate , 像素器范例 , QStyledItemDelegate ,和 QStyle .
此枚举描述委托可以赋予模型和视图组件的不同提示,以使用户体验舒适地在模型中编辑数据。
| 常量 | 值 | 描述 |
|---|---|---|
QAbstractItemDelegate::NoHint
|
0
|
没有推荐要履行的操作。 |
这些提示让委托影响视图的行为:
| 常量 | 值 | 描述 |
|---|---|---|
QAbstractItemDelegate::EditNextItem
|
1
|
视图应使用委托来打开下一视图项的编辑器。 |
QAbstractItemDelegate::EditPreviousItem
|
2
|
视图应使用委托来打开上一视图项的编辑器。 |
注意,自定义视图可能将下一和上一解释成不同概念。
以下提示最有用,当使用模型缓存数据时,譬如:操纵本地数据以提高性能 (或节省网络带宽) 的那些模型。
| 常量 | 值 | 描述 |
|---|---|---|
QAbstractItemDelegate::SubmitModelCache
|
3
|
若模型缓存数据,应将缓存数据写出到底层数据存储。 |
QAbstractItemDelegate::RevertModelCache
|
4
|
若模型缓存数据,应丢弃缓存数据并将其替换为来自底层数据存储的数据。 |
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.
创建新的抽象项委托采用给定 parent .
[虚拟]
QAbstractItemDelegate::
~QAbstractItemDelegate
()
销毁抽象项委托。
[signal]
void
QAbstractItemDelegate::
closeEditor
(
QWidget
*
editor
,
QAbstractItemDelegate::EndEditHint
hint
= NoHint)
此信号被发射,当用户已完成项编辑使用指定 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.
另请参阅 EndEditHint .
[signal]
void
QAbstractItemDelegate::
commitData
(
QWidget
*
editor
)
此信号必须被发射当 editor 小部件已完成数据编辑,且想要将其写回模型。
[虚拟]
QWidget
* QAbstractItemDelegate::
createEditor
(
QWidget
*
parent
, const
QStyleOptionViewItem
&
option
, const
QModelIndex
&
index
) const
返回用于编辑数据项的编辑器采用给定 index 。注意,索引包含正使用模型的有关信息。编辑器父级 Widget 的指定是通过 parent ,和项选项是通过 option .
The base implementation returns 0. If you want custom editing you will need to reimplement this function.
返回编辑器 Widget 应该拥有 Qt::StrongFocus ;否则, QMouseEvent s 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() ).
另请参阅 setModelData () 和 setEditorData ().
[虚拟]
bool
QAbstractItemDelegate::
editorEvent
(
QEvent
*
event
,
QAbstractItemModel
*
model
, const
QStyleOptionViewItem
&
option
, const
QModelIndex
&
index
)
当开始编辑项时,此函数被调用采用 event 触发编辑, model , index 对于项,和 option 用于渲染项。
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).
[slot]
bool
QAbstractItemDelegate::
helpEvent
(
QHelpEvent
*
event
,
QAbstractItemView
*
view
, const
QStyleOptionViewItem
&
option
, const
QModelIndex
&
index
)
每当出现帮助事件时,调用此函数采用 event view option 和 index 相当于出现事件的项。
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.
该函数在 Qt 4.3 引入。
另请参阅 QHelpEvent .
[pure virtual]
void
QAbstractItemDelegate::
paint
(
QPainter
*
painter
, const
QStyleOptionViewItem
&
option
, const
QModelIndex
&
index
) const
必须重实现此纯抽象函数,若想要提供自定义渲染。使用 painter 和样式 option 以渲染项指定通过项 index .
若重实现此,还必须重实现 sizeHint ().
[虚拟]
void
QAbstractItemDelegate::
setEditorData
(
QWidget
*
editor
, const
QModelIndex
&
index
) const
设置内容为给定 editor to the data for the item at the given index . Note that the index contains information about the model being used.
基实现什么都不做。若想要自定义编辑,需要重实现此函数。
另请参阅 setModelData ().
[虚拟]
void
QAbstractItemDelegate::
setModelData
(
QWidget
*
editor
,
QAbstractItemModel
*
model
, const
QModelIndex
&
index
) const
设置用于项的数据在给定 index 在 model 到内容为给定 editor .
基实现什么都不做。若想要自定义编辑,需要重实现此函数。
另请参阅 setEditorData ().
[pure virtual]
QSize
QAbstractItemDelegate::
sizeHint
(const
QStyleOptionViewItem
&
option
, const
QModelIndex
&
index
) const
必须重实现此纯抽象函数,若想要提供自定义渲染。选项的指定通过 option 和模型项通过 index .
若重实现此,还必须重实现 paint ().
[signal]
void
QAbstractItemDelegate::
sizeHintChanged
(const
QModelIndex
&
index
)
此信号必须被发射当 sizeHint () of index 改变。
视图自动连接到此信号并重新布局项,如有必要。
该函数在 Qt 4.4 引入。
[虚拟]
void
QAbstractItemDelegate::
updateEditorGeometry
(
QWidget
*
editor
, const
QStyleOptionViewItem
&
option
, const
QModelIndex
&
index
) const
更新几何体为 editor 对于项采用给定 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.
基实现什么都不做。若想要自定义编辑,必须重实现此函数。