<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html><head><title>QAbstractItemDelegate Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm } a:link { color: #004faf; text-decoration: none } a:visited { color: #672967; text-decoration: none } td.postheader { font-family: sans-serif } tr.address { font-family: sans-serif } body { background: #ffffff; color: black; } </style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">  </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QAbstractItemDelegate Class Reference<br /><sup><sup>[<a href="qtgui.html">QtGui</a> module]</sup></sup></h1><p>The QAbstractItemDelegate class is used to display and edit data items from a model. <a href="#details">More...</a></p> <p>Inherits <a href="qobject.html">QObject</a>.</p><p>Inherited by <a href="qitemdelegate.html">QItemDelegate</a> and <a href="qstyleditemdelegate.html">QStyledItemDelegate</a>.</p><h3>Types</h3><ul><li><div class="fn" />enum <b><a href="qabstractitemdelegate.html#EndEditHint-enum">EndEditHint</a></b> { NoHint, EditNextItem, EditPreviousItem, SubmitModelCache, RevertModelCache }</li></ul><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qabstractitemdelegate.html#QAbstractItemDelegate">__init__</a></b> (<i>self</i>, QObject <i>parent</i> = None)</li><li><div class="fn" />QWidget <b><a href="qabstractitemdelegate.html#createEditor">createEditor</a></b> (<i>self</i>, QWidget <i>parent</i>, QStyleOptionViewItem <i>option</i>, QModelIndex <i>index</i>)</li><li><div class="fn" />bool <b><a href="qabstractitemdelegate.html#editorEvent">editorEvent</a></b> (<i>self</i>, QEvent <i>event</i>, QAbstractItemModel <i>model</i>, QStyleOptionViewItem <i>option</i>, QModelIndex <i>index</i>)</li><li><div class="fn" />bool <b><a href="qabstractitemdelegate.html#helpEvent">helpEvent</a></b> (<i>self</i>, QHelpEvent <i>event</i>, QAbstractItemView <i>view</i>, QStyleOptionViewItem <i>option</i>, QModelIndex <i>index</i>)</li><li><div class="fn" /><b><a href="qabstractitemdelegate.html#paint">paint</a></b> (<i>self</i>, QPainter <i>painter</i>, QStyleOptionViewItem <i>option</i>, QModelIndex <i>index</i>)</li><li><div class="fn" /><b><a href="qabstractitemdelegate.html#setEditorData">setEditorData</a></b> (<i>self</i>, QWidget <i>editor</i>, QModelIndex <i>index</i>)</li><li><div class="fn" /><b><a href="qabstractitemdelegate.html#setModelData">setModelData</a></b> (<i>self</i>, QWidget <i>editor</i>, QAbstractItemModel <i>model</i>, QModelIndex <i>index</i>)</li><li><div class="fn" />QSize <b><a href="qabstractitemdelegate.html#sizeHint">sizeHint</a></b> (<i>self</i>, QStyleOptionViewItem <i>option</i>, QModelIndex <i>index</i>)</li><li><div class="fn" /><b><a href="qabstractitemdelegate.html#updateEditorGeometry">updateEditorGeometry</a></b> (<i>self</i>, QWidget <i>editor</i>, QStyleOptionViewItem <i>option</i>, QModelIndex <i>index</i>)</li></ul><h3>Static Methods</h3><ul><li><div class="fn" />QString <b><a href="qabstractitemdelegate.html#elidedText">elidedText</a></b> (QFontMetrics <i>fontMetrics</i>, int <i>width</i>, Qt.TextElideMode <i>mode</i>, QString <i>text</i>)</li></ul><h3>Qt Signals</h3><ul><li><div class="fn" />void <b><a href="qabstractitemdelegate.html#closeEditor">closeEditor</a></b> (QWidget *,QAbstractItemDelegate::EndEditHint = QAbstractItemDelegate.NoHint)</li><li><div class="fn" />void <b><a href="qabstractitemdelegate.html#commitData">commitData</a></b> (QWidget *)</li><li><div class="fn" />void <b><a href="qabstractitemdelegate.html#sizeHintChanged">sizeHintChanged</a></b> (const QModelIndex&)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QAbstractItemDelegate class is used to display and edit data items from a model.</p> <p>A 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.</p> <p>The QAbstractItemDelegate class is one of the <a href="model-view.html">Model/View Classes</a> and is part of Qt's <a href="model-view-programming.html">model/view framework</a>.</p> <p>To render an item in a custom way, you must implement <a href="qabstractitemdelegate.html#paint">paint</a>() and <a href="qabstractitemdelegate.html#sizeHint">sizeHint</a>(). The <a href="qitemdelegate.html">QItemDelegate</a> class provides default implementations for these functions; if you do not need custom rendering, subclass that class instead.</p> <p>We give an example of drawing a progress bar in items; in our case for a package management program.</p> <p class="centerAlign"><img alt="" src="images/widgetdelegate.png" /></p> <p>We create the <tt>WidgetDelegate</tt> class, which inherits from <a href="qstyleditemdelegate.html">QStyledItemDelegate</a>. We do the drawing in the <a href="qabstractitemdelegate.html#paint">paint</a>() function:</p> <pre class="cpp"> <span class="type">void</span> WidgetDelegate<span class="operator">.</span><a href="qabstractitemdelegate.html#paint">paint</a>(<span class="type"><a href="qpainter.html">QPainter</a></span> <span class="operator">*</span>painter<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qstyleoptionviewitem.html">QStyleOptionViewItem</a></span> <span class="operator">&</span>option<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qmodelindex.html">QModelIndex</a></span> <span class="operator">&</span>index) <span class="keyword">const</span> { <span class="keyword">if</span> (index<span class="operator">.</span>column() <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span>) { <span class="type">int</span> progress <span class="operator">=</span> index<span class="operator">.</span>data()<span class="operator">.</span>toInt(); <span class="type"><a href="qstyleoptionprogressbar.html">QStyleOptionProgressBar</a></span> progressBarOption; progressBarOption<span class="operator">.</span>rect <span class="operator">=</span> option<span class="operator">.</span>rect; progressBarOption<span class="operator">.</span>minimum <span class="operator">=</span> <span class="number">0</span>; progressBarOption<span class="operator">.</span>maximum <span class="operator">=</span> <span class="number">100</span>; progressBarOption<span class="operator">.</span>progress <span class="operator">=</span> progress; progressBarOption<span class="operator">.</span>text <span class="operator">=</span> <span class="type"><a href="qstring.html">QString</a></span><span class="operator">.</span>number(progress) <span class="operator">+</span> <span class="string">"%"</span>; progressBarOption<span class="operator">.</span>textVisible <span class="operator">=</span> <span class="keyword">true</span>; <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">.</span>style()<span class="operator">-</span><span class="operator">></span>drawControl(<span class="type"><a href="qstyle.html">QStyle</a></span><span class="operator">.</span>CE_ProgressBar<span class="operator">,</span> <span class="operator">&</span>progressBarOption<span class="operator">,</span> painter); } <span class="keyword">else</span> <span class="type"><a href="qstyleditemdelegate.html">QStyledItemDelegate</a></span><span class="operator">.</span>paint(painter<span class="operator">,</span> option<span class="operator">,</span> index); </pre> <p>Notice that we use a <a href="qstyleoptionprogressbar.html">QStyleOptionProgressBar</a> and initialize its members. We can then use the current <a href="qstyle.html">QStyle</a> to draw it.</p> <p>To provide custom editing, there are two approaches that can be used. The first approach is to create an editor widget and display it directly on top of the item. To do this you must reimplement <a href="qabstractitemdelegate.html#createEditor">createEditor</a>() to provide an editor widget, <a href="qabstractitemdelegate.html#setEditorData">setEditorData</a>() to populate the editor with the data from the model, and <a href="qabstractitemdelegate.html#setModelData">setModelData</a>() so that the delegate can update the model with data from the editor.</p> <p>The second approach is to handle user events directly by reimplementing <a href="qabstractitemdelegate.html#editorEvent">editorEvent</a>().</p> <hr /><h2>Type Documentation</h2><h3 class="fn"><a name="EndEditHint-enum" />QAbstractItemDelegate.EndEditHint</h3><p>This enum describes the different hints that the delegate can give to the model and view components to make editing data in a model a comfortable experience for the user.</p> <table class="valuelist"> <tr class="odd" valign="top"> <th class="tblConst">Constant</th> <th class="tblval">Value</th> <th class="tbldscr">Description</th> </tr> <tr> <td class="topAlign"><tt>QAbstractItemDelegate.NoHint</tt></td> <td class="topAlign"><tt>0</tt></td> <td class="topAlign">There is no recommended action to be performed.</td> </tr> </table> <p>These hints let the delegate influence the behavior of the view:</p> <table class="valuelist"> <tr class="even" valign="top"> <th class="tblConst">Constant</th> <th class="tblval">Value</th> <th class="tbldscr">Description</th> </tr> <tr> <td class="topAlign"> <tt>QAbstractItemDelegate.EditNextItem</tt></td> <td class="topAlign"><tt>1</tt></td> <td class="topAlign">The view should use the delegate to open an editor on the next item in the view.</td> </tr> <tr> <td class="topAlign"> <tt>QAbstractItemDelegate.EditPreviousItem</tt></td> <td class="topAlign"><tt>2</tt></td> <td class="topAlign">The view should use the delegate to open an editor on the previous item in the view.</td> </tr> </table> <p>Note that custom views may interpret the concepts of next and previous differently.</p> <p>The following hints are most useful when models are used that cache data, such as those that manipulate data locally in order to increase performance or conserve network bandwidth.</p> <table class="valuelist"> <tr class="odd" valign="top"> <th class="tblConst">Constant</th> <th class="tblval">Value</th> <th class="tbldscr">Description</th> </tr> <tr> <td class="topAlign"> <tt>QAbstractItemDelegate.SubmitModelCache</tt></td> <td class="topAlign"><tt>3</tt></td> <td class="topAlign">If the model caches data, it should write out cached data to the underlying data store.</td> </tr> <tr> <td class="topAlign"> <tt>QAbstractItemDelegate.RevertModelCache</tt></td> <td class="topAlign"><tt>4</tt></td> <td class="topAlign">If the model caches data, it should discard cached data and replace it with data from the underlying data store.</td> </tr> </table> <p>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.</p> <hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QAbstractItemDelegate" />QAbstractItemDelegate.__init__ (<i>self</i>, <a href="qobject.html">QObject</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Creates a new abstract item delegate with the given <i>parent</i>.</p> <h3 class="fn"><a name="createEditor" /><a href="qwidget.html">QWidget</a> QAbstractItemDelegate.createEditor (<i>self</i>, <a href="qwidget.html">QWidget</a> <i>parent</i>, <a href="qstyleoptionviewitem.html">QStyleOptionViewItem</a> <i>option</i>, <a href="qmodelindex.html">QModelIndex</a> <i>index</i>)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Returns the editor to be used for editing the data item with the given <i>index</i>. Note that the index contains information about the model being used. The editor's parent widget is specified by <i>parent</i>, and the item options by <i>option</i>.</p> <p>The base implementation returns 0. If you want custom editing you will need to reimplement this function.</p> <p>The returned editor widget should have <a href="qt.html#FocusPolicy-enum">Qt.StrongFocus</a>; otherwise, <a href="qmouseevent.html">QMouseEvent</a>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 <a href="qwidget.html#autoFillBackground-prop">setAutoFillBackground()</a>).</p> <p><b>See also</b> <a href="qabstractitemdelegate.html#setModelData">setModelData</a>() and <a href="qabstractitemdelegate.html#setEditorData">setEditorData</a>().</p> <h3 class="fn"><a name="editorEvent" />bool QAbstractItemDelegate.editorEvent (<i>self</i>, <a href="qevent.html">QEvent</a> <i>event</i>, <a href="qabstractitemmodel.html">QAbstractItemModel</a> <i>model</i>, <a href="qstyleoptionviewitem.html">QStyleOptionViewItem</a> <i>option</i>, <a href="qmodelindex.html">QModelIndex</a> <i>index</i>)</h3><p>When editing of an item starts, this function is called with the <i>event</i> that triggered the editing, the <i>model</i>, the <i>index</i> of the item, and the <i>option</i> used for rendering the item.</p> <p>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.</p> <p>The base implementation returns false (indicating that it has not handled the event).</p> <h3 class="fn"><a name="elidedText" />QString QAbstractItemDelegate.elidedText (<a href="qfontmetrics.html">QFontMetrics</a> <i>fontMetrics</i>, int <i>width</i>, <a href="qt.html#TextElideMode-enum">Qt.TextElideMode</a> <i>mode</i>, QString <i>text</i>)</h3><h3 class="fn"><a name="helpEvent" />bool QAbstractItemDelegate.helpEvent (<i>self</i>, <a href="qhelpevent.html">QHelpEvent</a> <i>event</i>, <a href="qabstractitemview.html">QAbstractItemView</a> <i>view</i>, <a href="qstyleoptionviewitem.html">QStyleOptionViewItem</a> <i>option</i>, <a href="qmodelindex.html">QModelIndex</a> <i>index</i>)</h3><p>This method is also a Qt slot with the C++ signature <tt>bool helpEvent(QHelpEvent *,QAbstractItemView *,const QStyleOptionViewItem&,const QModelIndex&)</tt>.</p><p>Whenever a help event occurs, this function is called with the <i>event</i> <i>view</i> <i>option</i> and the <i>index</i> that corresponds to the item where the event occurs.</p> <p>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.</p> <p>For <a href="qevent.html#Type-enum">QEvent.ToolTip</a> and <a href="qevent.html#Type-enum">QEvent.WhatsThis</a> events that were handled successfully, the relevant popup may be shown depending on the user's system configuration.</p> <p>This function was introduced in Qt 4.3.</p> <p><b>See also</b> <a href="qhelpevent.html">QHelpEvent</a>.</p> <h3 class="fn"><a name="paint" />QAbstractItemDelegate.paint (<i>self</i>, <a href="qpainter.html">QPainter</a> <i>painter</i>, <a href="qstyleoptionviewitem.html">QStyleOptionViewItem</a> <i>option</i>, <a href="qmodelindex.html">QModelIndex</a> <i>index</i>)</h3><p>This method is abstract and should be reimplemented in any sub-class.</p><p>This pure abstract function must be reimplemented if you want to provide custom rendering. Use the <i>painter</i> and style <i>option</i> to render the item specified by the item <i>index</i>.</p> <p>If you reimplement this you must also reimplement <a href="qabstractitemdelegate.html#sizeHint">sizeHint</a>().</p> <h3 class="fn"><a name="setEditorData" />QAbstractItemDelegate.setEditorData (<i>self</i>, <a href="qwidget.html">QWidget</a> <i>editor</i>, <a href="qmodelindex.html">QModelIndex</a> <i>index</i>)</h3><p>Sets the contents of the given <i>editor</i> to the data for the item at the given <i>index</i>. Note that the index contains information about the model being used.</p> <p>The base implementation does nothing. If you want custom editing you will need to reimplement this function.</p> <p><b>See also</b> <a href="qabstractitemdelegate.html#setModelData">setModelData</a>().</p> <h3 class="fn"><a name="setModelData" />QAbstractItemDelegate.setModelData (<i>self</i>, <a href="qwidget.html">QWidget</a> <i>editor</i>, <a href="qabstractitemmodel.html">QAbstractItemModel</a> <i>model</i>, <a href="qmodelindex.html">QModelIndex</a> <i>index</i>)</h3><p>Sets the data for the item at the given <i>index</i> in the <i>model</i> to the contents of the given <i>editor</i>.</p> <p>The base implementation does nothing. If you want custom editing you will need to reimplement this function.</p> <p><b>See also</b> <a href="qabstractitemdelegate.html#setEditorData">setEditorData</a>().</p> <h3 class="fn"><a name="sizeHint" /><a href="qsize.html">QSize</a> QAbstractItemDelegate.sizeHint (<i>self</i>, <a href="qstyleoptionviewitem.html">QStyleOptionViewItem</a> <i>option</i>, <a href="qmodelindex.html">QModelIndex</a> <i>index</i>)</h3><p>This method is abstract and should be reimplemented in any sub-class.</p><p>This pure abstract function must be reimplemented if you want to provide custom rendering. The options are specified by <i>option</i> and the model item by <i>index</i>.</p> <p>If you reimplement this you must also reimplement <a href="qabstractitemdelegate.html#paint">paint</a>().</p> <h3 class="fn"><a name="updateEditorGeometry" />QAbstractItemDelegate.updateEditorGeometry (<i>self</i>, <a href="qwidget.html">QWidget</a> <i>editor</i>, <a href="qstyleoptionviewitem.html">QStyleOptionViewItem</a> <i>option</i>, <a href="qmodelindex.html">QModelIndex</a> <i>index</i>)</h3><p>Updates the geometry of the <i>editor</i> for the item with the given <i>index</i>, according to the rectangle specified in the <i>option</i>. 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.</p> <p>The base implementation does nothing. If you want custom editing you must reimplement this function.</p> <hr /><h2>Qt Signal Documentation</h2><h3 class="fn"><a name="closeEditor" />void closeEditor (QWidget *,QAbstractItemDelegate::EndEditHint = QAbstractItemDelegate.NoHint)</h3><p>This is the default overload of this signal.</p><p>This signal is emitted when the user has finished editing an item using the specified <i>editor</i>.</p> <p>The <i>hint</i> 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 <tt>EditNextItem</tt> is specified, the view should use a delegate to open an editor on the next item in the model.</p> <p><b>See also</b> <a href="qabstractitemdelegate.html#EndEditHint-enum">EndEditHint</a>.</p> <h3 class="fn"><a name="commitData" />void commitData (QWidget *)</h3><p>This is the default overload of this signal.</p><p>This signal must be emitted when the <i>editor</i> widget has completed editing the data, and wants to write it back into the model.</p> <h3 class="fn"><a name="sizeHintChanged" />void sizeHintChanged (const QModelIndex&)</h3><p>This is the default overload of this signal.</p><p>This signal must be emitted when the <a href="qabstractitemdelegate.html#sizeHint">sizeHint</a>() of <i>index</i> changed.</p> <p>Views automatically connect to this signal and relayout items as necessary.</p> <p>This function was introduced in Qt 4.4.</p> <address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.10.3 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2012</td><td align="right" width="25%">Qt 4.8.5</td></tr></table></div></address></body></html>