Skip to content

PythonQt.Qt.QStyledItemDelegate

QStyledItemDelegate

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

When displaying data from models in Qt item views, e.g., a QTableView, the individual items are drawn by a delegate. Also, when an item is edited, it provides an editor widget, which is placed on top of the item view while editing takes place. QStyledItemDelegate is the default delegate for all Qt item views, and is installed upon them when they are created.

The QStyledItemDelegate class is one of the Model/View Classes and is part of Qt's model/view framework. The delegate allows the display and editing of items to be developed independently from the model and view.

The data of items in models are assigned an ItemDataRole; each item can store a QVariant for each role. QStyledItemDelegate implements display and editing for the most common datatypes expected by users, including booleans, integers, and strings.

The data will be drawn differently depending on which role they have in the model. The following table describes the roles and the data types the delegate can handle for each of them. It is often sufficient to ensure that the model returns appropriate data for each of the roles to determine the appearance of items in views.

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

Editors are created with a 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 EditRole that is edited. See the QItemEditorFactory class for a more high-level introduction to item editor factories. The Color Editor Factory example shows how to create custom editors with a factory.

Subclassing QStyledItemDelegate

If the delegate does not support painting of the data types you need or you want to customize the drawing of items, you need to subclass QStyledItemDelegate, and reimplement paint() and possibly sizeHint(). The paint() function is called individually for each item, and with sizeHint(), you can specify the hint for each of them.

When reimplementing paint(), one would typically handle the datatypes one would like to draw and use the superclass implementation for other types.

The painting of check box indicators are performed by the current style. The style also specifies the size and the bounding rectangles in which to draw the data for the different data roles. The bounding rectangle of the item itself is also calculated by the style. When drawing already supported datatypes, it is therefore a good idea to ask the style for these bounding rectangles. The QStyle class description describes this in more detail.

If you wish to change any of the bounding rectangles calculated by the style or the painting of check box indicators, you can subclass QStyle. Note, however, that the size of the items can also be affected by reimplementing sizeHint().

It is possible for a custom delegate to provide editors without the use of an editor item factory. In this case, the following virtual functions must be reimplemented:

  • 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 Star Delegate example creates editors by reimplementing these methods.

QStyledItemDelegate vs. QItemDelegate

Since Qt 4.4, there are two delegate classes: QItemDelegate and QStyledItemDelegate. However, the default delegate is 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.

If you wish to customize the painting of item views, you should implement a custom style. Please see the QStyle class documentation for details.

Source: https://doc.qt.io/archives/qt-5.15/qstyleditemdelegate.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
EditNextItem attribute Any (shared captured text)
EditPreviousItem attribute Any (shared captured text)
EndEditHint attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
NoHint attribute Any (shared captured text)
RevertModelCache attribute Any (shared captured text)
SubmitModelCache attribute Any (shared captured text)
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
blockSignals function (self, b: Any) -> bool X.blockSignals(b) -> bool (shared captured text)
childEvent function (self, event: Any) -> Any (shared captured text)
children function (self) -> tuple X.children() -> tuple (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
closeEditor function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.Qt.QStyledItemDelegate.closeEditor; argument details are not documented.
commitData function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.Qt.QStyledItemDelegate.commitData; argument details are not documented.
connect function (self, signal: Any, receiver: Any, slot: Any, type: Any) -> bool X.connect(signal, receiver, slot, type) -> bool (shared captured text)
createEditor function (self, parent: Any, option: Any, index: Any) -> Any X.createEditor(parent, option, index) -> PythonQt.QtGui.QWidget (shared captured text)
customEvent function (self, event: Any) -> Any (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
deleteLater function (self) -> Any (shared captured text)
destroyEditor function (self, editor: Any, index: Any) -> Any X.destroyEditor(editor, index) (shared captured text)
destroyed function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.Qt.QStyledItemDelegate.destroyed; argument details are not documented.
disconnect function (self, signal: Any, receiver: Any, slot: Any) -> bool X.disconnect(signal, receiver, slot) -> bool (shared captured text)
displayText function (self, value: Any, locale: Any) -> str X.displayText(value, locale) -> str (shared captured text)
dumpObjectInfo function (self) -> Any (shared captured text)
dumpObjectTree function (self) -> Any (shared captured text)
dynamicPropertyNames function (self) -> Any (shared captured text)
editorEvent function (self, event: Any, model: Any, option: Any, index: Any) -> bool X.editorEvent(event, model, option, index) -> bool (shared captured text)
event function (self, event: Any) -> bool X.event(event) -> bool (shared captured text)
eventFilter function (self, object: Any, event: Any) -> bool X.eventFilter(object, event) -> bool (shared captured text)
findChild function (self, type: Any, name: Any) -> Any X.findChild(type, name) -> PythonQt.private.QObject (shared captured text)
findChildren function (self, type: Any, name: Any) -> tuple X.findChildren(type, name) -> tuple (shared captured text)
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
helpEvent function (self, event: Any, view: Any, option: Any, index: Any) -> bool X.helpEvent(event, view, option, index) -> bool (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
initStyleOption function (self, option: Any, index: Any) -> None X.initStyleOption(option, index) (shared captured text)
installEventFilter function (self, filterObj: Any) -> Any (shared captured text)
isSignalConnected function (self, signal: Any) -> bool X.isSignalConnected(signal) -> bool (shared captured text)
isWidgetType function (self) -> bool X.isWidgetType() -> bool (shared captured text)
isWindowType function (self) -> bool X.isWindowType() -> bool (shared captured text)
itemEditorFactory function (self) -> Any X.itemEditorFactory() -> PythonQt.QtGui.QItemEditorFactory (shared captured text)
killTimer function (self, id: Any) -> Any (shared captured text)
metaObject function (self) -> Any X.metaObject() -> PythonQt.QtCore.QMetaObject (shared captured text)
moveToThread function (self, thread: Any) -> Any (shared captured text)
objectName attribute Any (shared captured text)
objectNameChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.Qt.QStyledItemDelegate.objectNameChanged; argument details are not documented.
paint function (self, painter: Any, option: Any, index: Any) -> None X.paint(painter, option, index) (shared captured text)
paintingRoles function (self) -> tuple X.paintingRoles() -> tuple (shared captured text)
parent function (self) -> Any X.parent() -> PythonQt.private.QObject (shared captured text)
property function (self, name: Any) -> Any X.property(name) -> object (shared captured text)
removeEventFilter function (self, obj: Any) -> Any (shared captured text)
sender function (self) -> Any X.sender() -> PythonQt.private.QObject (shared captured text)
senderSignalIndex function (self) -> int X.senderSignalIndex() -> int (shared captured text)
setEditorData function (self, editor: Any, index: Any) -> None X.setEditorData(editor, index) (shared captured text)
setItemEditorFactory function (self, factory: Any) -> None Sets the editor factory to be used by the item delegate to be the factory specified. (shared captured text)
setModelData function (self, editor: Any, model: Any, index: Any) -> None X.setModelData(editor, model, index) (shared captured text)
setObjectName function (self, name: Any) -> Any (shared captured text)
setParent function (self, parent: Any) -> Any (shared captured text)
setProperty function (self, name: Any, value: Any) -> bool X.setProperty(name, value) -> bool (shared captured text)
signalsBlocked function (self) -> bool X.signalsBlocked() -> bool (shared captured text)
sizeHint function (self, option: Any, index: Any) -> Any X.sizeHint(option, index) -> PythonQt.QtCore.QSize (shared captured text)
sizeHintChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.Qt.QStyledItemDelegate.sizeHintChanged; argument details are not documented.
startTimer function (self, interval: Any, timerType: Any) -> int X.startTimer(interval, timerType) -> int (shared captured text)
thread function (self) -> Any (shared captured text)
timerEvent function (self, event: Any) -> Any (shared captured text)
tr function (self, text: Any, ambig: Any, n: Any) -> str X.tr(text, ambig, n) -> str (shared captured text)
updateEditorGeometry function (self, editor: Any, option: Any, index: Any) -> None X.updateEditorGeometry(editor, option, index) (shared captured text)