QItemDelegate 類

The QItemDelegate 類為來自模型的數據項,提供顯示和編輯設施。 更多...

頭: #include <QItemDelegate>
qmake: QT += widgets
繼承: QAbstractItemDelegate

特性

公共函數

QItemDelegate (QObject * parent = nullptr)
virtual ~QItemDelegate ()
bool hasClipping () const
QItemEditorFactory * itemEditorFactory () const
void setClipping (bool clip )
void setItemEditorFactory (QItemEditorFactory * factory )

重實現公共函數

virtual QWidget * createEditor (QWidget * parent , const QStyleOptionViewItem & option , const QModelIndex & index ) const override
virtual void paint (QPainter * painter , const QStyleOptionViewItem & option , const QModelIndex & index ) const override
virtual void setEditorData (QWidget * editor , const QModelIndex & index ) const override
virtual void setModelData (QWidget * editor , QAbstractItemModel * model , const QModelIndex & index ) const override
virtual QSize sizeHint (const QStyleOptionViewItem & option , const QModelIndex & index ) const override
virtual void updateEditorGeometry (QWidget * editor , const QStyleOptionViewItem & option , const QModelIndex & index ) const override

靜態公共成員

const QMetaObject staticMetaObject

保護函數

void drawBackground (QPainter * painter , const QStyleOptionViewItem & option , const QModelIndex & index ) const
virtual void drawCheck (QPainter * painter , const QStyleOptionViewItem & option , const QRect & rect , Qt::CheckState state ) const
virtual void drawDecoration (QPainter * painter , const QStyleOptionViewItem & option , const QRect & rect , const QPixmap & pixmap ) const
virtual void drawDisplay (QPainter * painter , const QStyleOptionViewItem & option , const QRect & rect , const QString & text ) const
virtual void drawFocus (QPainter * painter , const QStyleOptionViewItem & option , const QRect & rect ) const

重實現保護函數

virtual bool editorEvent (QEvent * event , QAbstractItemModel * model , const QStyleOptionViewItem & option , const QModelIndex & index ) override
virtual bool eventFilter (QObject * editor , QEvent * event ) override

額外繼承成員

詳細描述

The QItemDelegate 類為來自模型的數據項,提供顯示和編輯設施。

QItemDelegate 可以用於提供自定義顯示特徵和編輯器 Widget 為項視圖基於 QAbstractItemView 子類。為此用途使用委托允許定製顯示和編輯機製,且開發獨立於模型/視圖。

The QItemDelegate 類是一種 模型/視圖類 且屬於 Qt 的 模型/視圖框架 。注意, QStyledItemDelegate 已接管繪製 Qt 項視圖的作業。推薦使用 QStyledItemDelegate 當創建新委托時。

在標準視圖顯示來自自定義模型的項時,經常是足夠的,隻需確保模型返迴適當數據為各 roles 以確定視圖中的項外觀。用於 Qt 標準視圖的默認委托,使用此角色信息以用戶期望的大多數常見形式顯示項。不管怎樣,有時甚至有必要擁有更多項外觀控製,相比默認委托可以提供的。

此類提供用於在視圖中描繪項數據,和編輯來自項模型的數據的函數的默認實現。默認實現的 paint () 和 sizeHint () 虛函數,定義在 QAbstractItemDelegate ,的提供以確保委托實現視圖所期望的正確基本行為。可以在子類中重實現這些函數,以定製項外觀。

當編輯項視圖中的數據時, QItemDelegate 提供編輯器 Widget,是編輯發生時放置在視圖頂部的 Widget。編輯器的創建是采用 QItemEditorFactory ; a default static instance provided by QItemEditorFactory is installed on all item delegates. You can set a custom factory using setItemEditorFactory () or set a new default factory with QItemEditorFactory::setDefaultFactory (). It is the data stored in the item model with the Qt::EditRole that is edited.

Only the standard editing functions for widget-based delegates are reimplemented here:

  • createEditor () returns the widget used to change data from the model and can be reimplemented to customize editing behavior.
  • setEditorData () provides the widget with data to manipulate.
  • updateEditorGeometry () ensures that the editor is displayed correctly with respect to the item view.
  • setModelData () returns updated data to the model.

The closeEditor () signal indicates that the user has completed editing the data, and that the editor widget can be destroyed.

標準角色和數據類型

The default delegate used by the standard views supplied with Qt associates each standard role (defined by Qt::ItemDataRole ) with certain data types. Models that return data in these types can influence the appearance of the delegate as described in the following table.

角色 接受類型
Qt::BackgroundRole QBrush
Qt::BackgroundColorRole QColor (過時;使用 Qt::BackgroundRole 代替)
Qt::CheckStateRole Qt::CheckState
Qt::DecorationRole QIcon , QPixmap and QColor
Qt::DisplayRole QString 和按字符串錶示的類型
Qt::EditRole QItemEditorFactory 瞭解細節
Qt::FontRole QFont
Qt::SizeHintRole QSize
Qt::TextAlignmentRole Qt::Alignment
Qt::ForegroundRole QBrush
Qt::TextColorRole QColor (過時;使用 Qt::ForegroundRole 代替)

If the default delegate does not allow the level of customization that you need, either for display purposes or for editing data, it is possible to subclass QItemDelegate to implement the desired behavior.

子類化

當子類化 QItemDelegate to create a delegate that displays items using a custom renderer, it is important to ensure that the delegate can render items suitably for all the required states; e.g. selected, disabled, checked. The documentation for the paint () function contains some hints to show how this can be achieved.

可以提供自定義編輯器通過使用 QItemEditorFactory 色彩編輯器工廠範例 shows how a custom editor can be made available to delegates with the default item editor factory. This way, there is no need to subclass QItemDelegate . An alternative is to reimplement createEditor (), setEditorData (), setModelData (),和 updateEditorGeometry (). This process is described in the 自鏇框委托範例 .

QStyledItemDelegate vs. QItemDelegate

從 Qt 4.4 起,有 2 個委托類: QItemDelegate and QStyledItemDelegate 。不管怎樣,默認委托為 QStyledItemDelegate . These two classes are independent alternatives to painting and providing editors for items in views. The difference between them is that QStyledItemDelegate uses the current style to paint its items. We therefore recommend using QStyledItemDelegate as the base class when implementing custom delegates or when working with Qt style sheets. The code required for either class should be equal unless the custom delegate needs to use the style for drawing.

另請參閱 委托類 , QStyledItemDelegate , QAbstractItemDelegate , 自鏇框委托範例 , 設置編輯器範例 ,和 圖標範例 .

特性文檔編製

clipping : bool

若委托應裁剪描繪事件

This property will set the paint clip to the size of the item. The default value is on. It is useful for cases such as when images are larger than the size of the item.

該特性在 Qt 4.2 引入。

訪問函數:

bool hasClipping () const
void setClipping (bool clip )

成員函數文檔編製

QItemDelegate:: QItemDelegate ( QObject * parent = nullptr)

構造項委托采用給定 parent .

[虛擬] QItemDelegate:: ~QItemDelegate ()

銷毀項委托。

[override virtual] QWidget *QItemDelegate:: createEditor ( QWidget * parent , const QStyleOptionViewItem & option , const QModelIndex & index ) const

重實現自 QAbstractItemDelegate::createEditor ().

Returns the widget used to edit the item specified by index for editing. The parent widget and style option are used to control how the editor widget appears.

另請參閱 QAbstractItemDelegate::createEditor ().

[protected] void QItemDelegate:: drawBackground ( QPainter * painter , const QStyleOptionViewItem & option , const QModelIndex & index ) const

渲染項背景為給定 index ,使用給定 painter 和樣式 option .

該函數在 Qt 4.2 引入。

[virtual protected] void QItemDelegate:: drawCheck ( QPainter * painter , const QStyleOptionViewItem & option , const QRect & rect , Qt::CheckState state ) const

Renders a check indicator within the rectangle specified by rect ,使用給定 painter 和樣式 option ,使用給定 state .

[virtual protected] void QItemDelegate:: drawDecoration ( QPainter * painter , const QStyleOptionViewItem & option , const QRect & rect , const QPixmap & pixmap ) const

渲染裝飾 pixmap 在矩形內指定通過 rect 使用給定 painter 和樣式 option .

[virtual protected] void QItemDelegate:: drawDisplay ( QPainter * painter , const QStyleOptionViewItem & option , const QRect & rect , const QString & text ) const

渲染項視圖 text 在矩形內指定通過 rect 使用給定 painter 和樣式 option .

[virtual protected] void QItemDelegate:: drawFocus ( QPainter * painter , const QStyleOptionViewItem & option , const QRect & rect ) const

Renders the region within the rectangle specified by rect , indicating that it has the focus, using the given painter 和樣式 option .

[override virtual protected] bool QItemDelegate:: editorEvent ( QEvent * event , QAbstractItemModel * model , const QStyleOptionViewItem & option , const QModelIndex & index )

重實現自 QAbstractItemDelegate::editorEvent ().

[override virtual protected] bool QItemDelegate:: eventFilter ( QObject * editor , QEvent * event )

重實現自 QObject::eventFilter ().

返迴 true 若給定 editor 有效 QWidget 和給定 event 有處理;否則返迴 false . The following key press events are handled by default:

  • Tab
  • Backtab
  • Enter
  • 返迴
  • Esc

In the case of Tab , Backtab , Enter and 返迴 key press events, the editor 's data is committed to the model and the editor is closed. If the event Tab key press the view will open an editor on the next item in the view. Likewise, if the event Backtab key press the view will open an editor on the previous item in the view.

If the event is a Esc key press event, the editor is closed without committing its data.

另請參閱 commitData () 和 closeEditor ().

QItemEditorFactory *QItemDelegate:: itemEditorFactory () const

Returns the editor factory used by the item delegate. If no editor factory is set, the function will return null.

另請參閱 setItemEditorFactory ().

[override virtual] void QItemDelegate:: paint ( QPainter * painter , const QStyleOptionViewItem & option , const QModelIndex & index ) const

重實現自 QAbstractItemDelegate::paint ().

Renders the delegate using the given painter 和樣式 option 為項指定通過 index .

When reimplementing this function in a subclass, you should update the area held by the option's rect variable, using the option's state variable to determine the state of the item to be displayed, and adjust the way it is painted accordingly.

For example, a selected item may need to be displayed differently to unselected items, as shown in the following code:

    if (option.state & QStyle::State_Selected)
        painter->fillRect(option.rect, option.palette.highlight());
    int size = qMin(option.rect.width(), option.rect.height());
    int brightness = index.model()->data(index, Qt::DisplayRole).toInt();
    double radius = (size / 2.0) - (brightness / 255.0 * size / 2.0);
    if (radius == 0.0)
        return;
    painter->save();
    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->setPen(Qt::NoPen);
    if (option.state & QStyle::State_Selected)
        painter->setBrush(option.palette.highlightedText());
    else
    ...
					

After painting, you should ensure that the painter is returned to its the state it was supplied in when this function was called. For example, it may be useful to call QPainter::save () before painting and QPainter::restore () afterwards.

另請參閱 QStyle::State .

[override virtual] void QItemDelegate:: setEditorData ( QWidget * editor , const QModelIndex & index ) const

重實現自 QAbstractItemDelegate::setEditorData ().

Sets the data to be displayed and edited by the editor from the data model item specified by the model index .

The default implementation stores the data in the editor 小部件的 用戶特性 .

另請參閱 QMetaProperty::isUser ().

void QItemDelegate:: setItemEditorFactory ( QItemEditorFactory * factory )

Sets the editor factory to be used by the item delegate to be the factory specified. If no editor factory is set, the item delegate will use the default editor factory.

另請參閱 itemEditorFactory ().

[override virtual] void QItemDelegate:: setModelData ( QWidget * editor , QAbstractItemModel * model , const QModelIndex & index ) const

重實現自 QAbstractItemDelegate::setModelData ().

獲取數據從 editor 小部件並把它存儲在指定 model 在項 index .

The default implementation gets the value to be stored in the data model from the editor 小部件的 用戶特性 .

另請參閱 QMetaProperty::isUser ().

[override virtual] QSize QItemDelegate:: sizeHint (const QStyleOptionViewItem & option , const QModelIndex & index ) const

重實現自 QAbstractItemDelegate::sizeHint ().

返迴委托要顯示項所需的大小指定通過 index ,考慮樣式信息提供通過 option .

When reimplementing this function, note that in case of text items, QItemDelegate adds a margin (i.e. 2 * QStyle::PM_FocusFrameHMargin ) to the length of the text.

[override virtual] void QItemDelegate:: updateEditorGeometry ( QWidget * editor , const QStyleOptionViewItem & option , const QModelIndex & index ) const

重實現自 QAbstractItemDelegate::updateEditorGeometry ().

更新 editor 為項指定通過 index 根據樣式 option 給定。