QAbstractItemDelegate 類用於顯示和編輯來自模型的數據項。 更多...
| 頭: | #include <QAbstractItemDelegate> |
| qmake: | QT += widgets |
| 繼承: | QObject |
| 繼承者: |
| enum | EndEditHint { NoHint, EditNextItem, EditPreviousItem, SubmitModelCache, RevertModelCache } |
| QAbstractItemDelegate (QObject * parent = nullptr) | |
| 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 |
| void | closeEditor (QWidget * editor , QAbstractItemDelegate::EndEditHint hint = NoHint) |
| void | commitData (QWidget * editor ) |
| void | sizeHintChanged (const QModelIndex & index ) |
QAbstractItemDelegate 為模型/視圖體係結構中的委托,提供接口和常見功能。委托在視圖中顯示單個項,並處理模型數據的編輯。
QAbstractItemDelegate 類是一種 模型/視圖類 且屬於 Qt 的 模型/視圖框架 .
要以自定義方式渲染項,必須實現 paint () 和 sizeHint ()。 QStyledItemDelegate 類為這些函數提供默認實現;若不需要自定義渲染,以子類化該類取而代之。
範例,在項中繪製進度條;在包管理程序範例中。
創建
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 ().
另請參閱 模型/視圖編程 , QStyledItemDelegate , 像素器範例 , QStyledItemDelegate ,和 QStyle .
此枚舉描述委托可以賦予模型和視圖組件的不同提示,以使用戶體驗舒適地在模型中編輯數據。
| 常量 | 值 | 描述 |
|---|---|---|
QAbstractItemDelegate::NoHint
|
0
|
沒有推薦要履行的操作。 |
這些提示讓委托影響視圖的行為:
| 常量 | 值 | 描述 |
|---|---|---|
QAbstractItemDelegate::EditNextItem
|
1
|
視圖應使用委托來打開下一視圖項的編輯器。 |
QAbstractItemDelegate::EditPreviousItem
|
2
|
視圖應使用委托來打開上一視圖項的編輯器。 |
注意,自定義視圖可能將下一和上一解釋成不同概念。
以下提示最有用,當使用模型緩存數據時,譬如:操縱本地數據以提高性能 (或節省網絡帶寬) 的那些模型。
| 常量 | 值 | 描述 |
|---|---|---|
QAbstractItemDelegate::SubmitModelCache
|
3
|
若模型緩存數據,應將緩存數據寫齣到底層數據存儲。 |
QAbstractItemDelegate::RevertModelCache
|
4
|
若模型緩存數據,應丟棄緩存數據並將其替換為來自底層數據存儲的數據。 |
盡管模型和視圖應以適當方式響應這些提示,但若它們不相關,自定義組件可能忽略它們中的任何一個 (或全部)。
創建新的抽象項委托采用給定 parent .
[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 小部件已完成數據編輯,且想要將其寫迴模型。
[signal]
void
QAbstractItemDelegate::
sizeHintChanged
(const
QModelIndex
&
index
)
此信號必須被發射當 sizeHint () of index 改變。
視圖自動連接到此信號並重新布局項,如有必要。
該函數在 Qt 4.4 引入。
[虛擬]
QAbstractItemDelegate::
~QAbstractItemDelegate
()
銷毀抽象項委托。
[虛擬]
QWidget
*QAbstractItemDelegate::
createEditor
(
QWidget
*
parent
, const
QStyleOptionViewItem
&
option
, const
QModelIndex
&
index
) const
返迴用於編輯數據項的編輯器采用給定 index 。注意,索引包含正使用模型的有關信息。編輯器父級 Widget 的指定是通過 parent ,和項選項是通過 option .
基實現返迴
nullptr
。若想要自定義編輯,需要重實現此函數。
返迴編輯器 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() ).
另請參閱 destroyEditor (), setModelData (),和 setEditorData ().
[虛擬]
void
QAbstractItemDelegate::
destroyEditor
(
QWidget
*
editor
, const
QModelIndex
&
index
) const
被調用當 editor 不再需要編輯數據項采用給定 index 且應該被銷毀。默認行為是調用 deleteLater 在編輯器。如,通過重實現此函數來避免這種刪除是可能的。
該函數在 Qt 5.0 引入。
另請參閱 createEditor ().
[虛擬]
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.
基實現返迴
false
(指示它沒有處理事件)。
[虛擬]
bool
QAbstractItemDelegate::
helpEvent
(
QHelpEvent
*
event
,
QAbstractItemView
*
view
, const
QStyleOptionViewItem
&
option
, const
QModelIndex
&
index
)
每當齣現幫助事件時,調用此函數采用 event view option 和 index 相當於齣現事件的項。
返迴
true
若委托可以處理事件;否則返迴
false
。返迴 true 值指示使用索引獲得的數據,擁有要求角色。
For QEvent::ToolTip and QEvent::WhatsThis 事件被成功處理,可能展示相關彈齣窗口,從屬用戶係統配置。
該函數在 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 ().
[虛擬]
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.
基實現什麼都不做。若想要自定義編輯,必須重實現此函數。