QItemDelegate

Inherited by: QSqlRelationalDelegate

Synopsis

Functions

Virtual functions

Detailed Description

The PySide.QtGui.QItemDelegate class provides display and editing facilities for data items from a model.

PySide.QtGui.QItemDelegate can be used to provide custom display features and editor widgets for item views based on PySide.QtGui.QAbstractItemView subclasses. Using a delegate for this purpose allows the display and editing mechanisms to be customized and developed independently from the model and view.

The PySide.QtGui.QItemDelegate class is one of the Model/View Classes and is part of Qt’s model/view framework . Note that PySide.QtGui.QStyledItemDelegate has taken over the job of drawing Qt’s item views. We recommend the use of PySide.QtGui.QStyledItemDelegate when creating new delegates.

When displaying items from a custom model in a standard view, it is often sufficient to simply ensure that the model returns appropriate data for each of the roles that determine the appearance of items in views. The default delegate used by Qt’s standard views uses this role information to display items in most of the common forms expected by users. However, it is sometimes necessary to have even more control over the appearance of items than the default delegate can provide.

This class provides default implementations of the functions for painting item data in a view and editing data from item models. Default implementations of the PySide.QtGui.QItemDelegate.paint() and PySide.QtGui.QItemDelegate.sizeHint() virtual functions, defined in PySide.QtGui.QAbstractItemDelegate , are provided to ensure that the delegate implements the correct basic behavior expected by views. You can reimplement these functions in subclasses to customize the appearance of items.

When editing data in an item view, PySide.QtGui.QItemDelegate provides an editor widget, which is a widget that is placed on top of the view while editing takes place. Editors are created with a PySide.QtGui.QItemEditorFactory ; a default static instance provided by PySide.QtGui.QItemEditorFactory is installed on all item delegates. You can set a custom factory using PySide.QtGui.QItemDelegate.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:

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

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

Standard Roles and Data Types

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.

Role Accepted Types
Qt.BackgroundRole PySide.QtGui.QBrush
Qt.BackgroundColorRole PySide.QtGui.QColor (obsolete; use Qt.BackgroundRole instead)
Qt.CheckStateRole Qt.CheckState
Qt.DecorationRole PySide.QtGui.QIcon , PySide.QtGui.QPixmap and PySide.QtGui.QColor
Qt.DisplayRole PySide.QtCore.QString and types with a string representation
Qt.EditRole See PySide.QtGui.QItemEditorFactory for details
Qt.FontRole PySide.QtGui.QFont
Qt.SizeHintRole PySide.QtCore.QSize
Qt.TextAlignmentRole Qt.Alignment
Qt.ForegroundRole PySide.QtGui.QBrush
Qt.TextColorRole PySide.QtGui.QColor (obsolete; use Qt.ForegroundRole instead)

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 PySide.QtGui.QItemDelegate to implement the desired behavior.

Subclassing

When subclassing PySide.QtGui.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 PySide.QtGui.QItemDelegate.paint() function contains some hints to show how this can be achieved.

You can provide custom editors by using a PySide.QtGui.QItemEditorFactory . The Color Editor Factory Example 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 PySide.QtGui.QItemDelegate . An alternative is to reimplement PySide.QtGui.QItemDelegate.createEditor() , PySide.QtGui.QItemDelegate.setEditorData() , PySide.QtGui.QItemDelegate.setModelData() , and PySide.QtGui.QItemDelegate.updateEditorGeometry() . This process is described in the Spin Box Delegate Example .

QStyledItemDelegate vs. QItemDelegate

Since Qt 4.4, there are two delegate classes: PySide.QtGui.QItemDelegate and PySide.QtGui.QStyledItemDelegate . However, the default delegate is PySide.QtGui.QStyledItemDelegate . These two classes are independent alternatives to painting and providing editors for items in views. The difference between them is that PySide.QtGui.QStyledItemDelegate uses the current style to paint its items. We therefore recommend using PySide.QtGui.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.

See also

Delegate Classes PySide.QtGui.QStyledItemDelegate PySide.QtGui.QAbstractItemDelegate Spin Box Delegate Example Settings Editor Example Icons Example

class PySide.QtGui.QItemDelegate([parent=None])
Parameters:parentPySide.QtCore.QObject

Constructs an item delegate with the given parent .

PySide.QtGui.QItemDelegate.check(option, bounding, variant)
Parameters:
Return type:

PySide.QtCore.QRect

Note that on Mac, if /usr/include/AssertMacros.h is included prior to PySide.QtGui.QItemDelegate , and the application is building in debug mode, the check(assertion) will conflict with QItemDelegate::check.

To avoid this problem, add

#ifdef check #undef check #endif

after including AssertMacros.h

PySide.QtGui.QItemDelegate.decoration(option, variant)
Parameters:
Return type:

PySide.QtGui.QPixmap

Returns the pixmap used to decorate the root of the item view. The style option controls the appearance of the root; the variant refers to the data associated with an item.

PySide.QtGui.QItemDelegate.drawBackground(painter, option, index)
Parameters:

Renders the item background for the given index , using the given painter and style option .

PySide.QtGui.QItemDelegate.drawCheck(painter, option, rect, state)
Parameters:
PySide.QtGui.QItemDelegate.drawDecoration(painter, option, rect, pixmap)
Parameters:

Renders the decoration pixmap within the rectangle specified by rect using the given painter and style option .

PySide.QtGui.QItemDelegate.drawDisplay(painter, option, rect, text)
Parameters:

Renders the item view text within the rectangle specified by rect using the given painter and style option .

PySide.QtGui.QItemDelegate.drawFocus(painter, option, rect)
Parameters:

Renders the region within the rectangle specified by rect , indicating that it has the focus, using the given painter and style option .

PySide.QtGui.QItemDelegate.hasClipping()
Return type:PySide.QtCore.bool

This property holds if the delegate should clip the paint events.

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.

PySide.QtGui.QItemDelegate.itemEditorFactory()
Return type:PySide.QtGui.QItemEditorFactory

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

PySide.QtGui.QItemDelegate.rect(option, index, role)
Parameters:
Return type:

PySide.QtCore.QRect

PySide.QtGui.QItemDelegate.setClipping(clip)
Parameters:clipPySide.QtCore.bool

This property holds if the delegate should clip the paint events.

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.

PySide.QtGui.QItemDelegate.setItemEditorFactory(factory)
Parameters:factoryPySide.QtGui.QItemEditorFactory

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.

PySide.QtGui.QItemDelegate.setOptions(index, option)
Parameters:
Return type:

PySide.QtGui.QStyleOptionViewItem

PySide.QtGui.QItemDelegate.textRectangle(painter, rect, font, text)
Parameters:
Return type:

PySide.QtCore.QRect