继承者: QItemDelegate , QSqlRelationalDelegate , QStyledItemDelegate
PySide.QtGui.QAbstractItemDelegate class is used to display and edit data items from a model.
A PySide.QtGui.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.
PySide.QtGui.QAbstractItemDelegate class is one of the 模型/视图类 且属于 Qt 的 模型/视图框架 .
要以自定义方式渲染项,必须实现 PySide.QtGui.QAbstractItemDelegate.paint() and PySide.QtGui.QAbstractItemDelegate.sizeHint() 。 PySide.QtGui.QItemDelegate 类为这些函数提供默认实现;若不需要自定义渲染,以子类化该类取而代之。
范例,在项中绘制进度条;在包管理程序范例中。
创建 WidgetDelegate 类,继承自 PySide.QtGui.QStyledItemDelegate 。履行绘制在 PySide.QtGui.QAbstractItemDelegate.paint() 函数:
class WidgetDelegate (QStyledItemDelegate):
# ...
def paint(painter, option, index):
if index.column() == 1:
progress = index.data().toInt()
progressBarOption = QStyleOptionProgressBar()
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(self, painter, option, index)
注意:我们使用 PySide.QtGui.QStyleOptionProgressBar 并初始化其成员。然后可以使用当前 PySide.QtGui.QStyle 来绘制它。
要提供自定义编辑,有 2 种方式可以使用。第 1 种方式是创建编辑器 Widget 并将其直接显示在项顶部。要做到这点,必须重实现 PySide.QtGui.QAbstractItemDelegate.createEditor() to provide an editor widget, PySide.QtGui.QAbstractItemDelegate.setEditorData() to populate the editor with the data from the model, and PySide.QtGui.QAbstractItemDelegate.setModelData() so that the delegate can update the model with data from the editor.
第 2 种方式是直接处理用户事件通过重实现 PySide.QtGui.QAbstractItemDelegate.editorEvent() .
另请参阅
模型/视图编程 PySide.QtGui.QItemDelegate 像素器范例 PySide.QtGui.QStyledItemDelegate PySide.QtGui.QStyle
| 参数: | parent – PySide.QtCore.QObject |
|---|
创建新的抽象项委托采用给定 parent .
此枚举描述委托可以赋予模型和视图组件的不同提示,以使用户体验舒适地在模型中编辑数据。
| 常量 | 描述 |
|---|---|
| QAbstractItemDelegate.NoHint | 没有推荐要履行的操作。 |
这些提示让委托影响视图的行为:
| 常量 | 描述 |
|---|---|
| QAbstractItemDelegate.EditNextItem | 视图应使用委托来打开下一视图项的编辑器。 |
| QAbstractItemDelegate.EditPreviousItem | 视图应使用委托来打开上一视图项的编辑器。 |
注意,自定义视图可能将下一和上一解释成不同概念。
以下提示最有用,当使用模型缓存数据时,譬如:操纵本地数据以提高性能 (或节省网络带宽) 的那些模型。
| 常量 | 描述 |
|---|---|
| QAbstractItemDelegate.SubmitModelCache | 若模型缓存数据,应将缓存数据写出到底层数据存储。 |
| QAbstractItemDelegate.RevertModelCache | 若模型缓存数据,应丢弃缓存数据并将其替换为来自底层数据存储的数据。 |
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.
| 参数: |
|
|---|
| 参数: | editor – PySide.QtGui.QWidget |
|---|
| 参数: |
|
|---|---|
| 返回类型: |
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 .
The base implementation returns 0. If you want custom editing you will need to reimplement this function.
返回编辑器 Widget 应该拥有 Qt.StrongFocus ;否则, PySide.QtGui.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 PySide.QtGui.QWidget.setAutoFillBackground() ).
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
当开始编辑项时,此函数被调用采用 event 触发编辑, model , index 对于项,和 option 用于渲染项。
Mouse events are sent to PySide.QtGui.QAbstractItemDelegate.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).
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
Whenever a help event occurs, this function is called with the event view option 和 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 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 .
若重实现此,还必须重实现 PySide.QtGui.QAbstractItemDelegate.sizeHint() .
| 参数: |
|
|---|
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.
基实现什么都不做。若想要自定义编辑,需要重实现此函数。
| 参数: |
|
|---|
设置用于项的数据在给定 index 在 model 到内容为给定 editor .
基实现什么都不做。若想要自定义编辑,需要重实现此函数。
| 参数: |
|
|---|---|
| 返回类型: |
必须重实现此纯抽象函数,若想要提供自定义渲染。选项的指定通过 option 和模型项通过 index .
若重实现此,还必须重实现 PySide.QtGui.QAbstractItemDelegate.paint() .
| 参数: | arg__1 – PySide.QtCore.QModelIndex |
|---|
| 参数: |
|
|---|
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.
基实现什么都不做。若想要自定义编辑,必须重实现此函数。