Qt Jambi Home

com.trolltech.qt.gui
Class QItemDelegate

java.lang.Object
  extended by com.trolltech.qt.QSignalEmitter
      extended by com.trolltech.qt.QtJambiObject
          extended by com.trolltech.qt.core.QObject
              extended by com.trolltech.qt.gui.QAbstractItemDelegate
                  extended by com.trolltech.qt.gui.QItemDelegate
All Implemented Interfaces:
QtJambiInterface

public class QItemDelegate
extends QAbstractItemDelegate

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

QItemDelegate can be used to provide custom display features and editor widgets for item views based on 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 QItemDelegate class is one of the Model/View Classes and is part of Qt's model/view framework.

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 paint and sizeHint virtual functions, defined in 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, 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 QItemEditorFactory; a default static instance provided by QItemEditorFactory is installed on all item delagates. 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:

The 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.

RoleAccepted Types
Qt::BackgroundRoleQBrush
Qt::BackgroundColorRoleQColor (obsolete; use Qt::BackgroundRole instead)
Qt::CheckStateRoleQt::CheckState
Qt::DecorationRoleQIcon and QColor
Qt::DisplayRoleQString and types with a string representation
Qt::EditRoleSee QItemEditorFactory for details
Qt::FontRoleQFont
Qt::SizeHintRoleQSize
Qt::TextAlignmentRoleQt::Alignment
Qt::ForegroundRoleQBrush
Qt::TextColorRoleQColor (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 QItemDelegate to implement the desired behavior.

Subclassing

When subclassing 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.

You can provide custom editors by using a 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 QItemDelegate. An alternative is to reimplement createEditor, setEditorData, setModelData, and updateEditorGeometry. This process is described in the Spin Box Delegate example.

See Also:
Delegate Classes, QAbstractItemDelegate, Box Delegate Example, Editor Example, Example

Nested Class Summary
 
Nested classes/interfaces inherited from class com.trolltech.qt.gui.QAbstractItemDelegate
QAbstractItemDelegate.EndEditHint
 
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I>
 
Field Summary
 
Fields inherited from class com.trolltech.qt.gui.QAbstractItemDelegate
closeEditor, commitData
 
Constructor Summary
QItemDelegate()
          Equivalent to QItemDelegate(0).
QItemDelegate(QObject parent)
          Constructs an item delegate with the given parent.
 
Method Summary
protected  QRect check(QStyleOptionViewItem option, QRect bounding, java.lang.Object variant)
          
 QWidget createEditor(QWidget parent, QStyleOptionViewItem option, QModelIndex index)
          Returns the widget used to edit the item specified by index for editing.
protected  QPixmap decoration(QStyleOptionViewItem option, java.lang.Object variant)
          
protected  void drawBackground(QPainter painter, QStyleOptionViewItem option, QModelIndex index)
          Renders the item background for the given index, using the given painter and style option.
protected  void drawCheck(QPainter painter, QStyleOptionViewItem option, QRect rect, Qt.CheckState state)
          Renders a check indicator within the rectangle specified by rect, using the given painter and style option, using the given state.
protected  void drawDecoration(QPainter painter, QStyleOptionViewItem option, QRect rect, QPixmap pixmap)
          Renders the decoration pixmap within the rectangle specified by rect using the given painter and style option.
protected  void drawDisplay(QPainter painter, QStyleOptionViewItem option, QRect rect, java.lang.String text)
          Renders the item view text within the rectangle specified by rect using the given painter and style option.
protected  void drawFocus(QPainter painter, QStyleOptionViewItem option, QRect rect)
          Renders the region within the rectangle specified by rect, indicating that it has the focus, using the given painter and style option.
 boolean editorEvent(QEvent event, QAbstractItemModel model, QStyleOptionViewItem option, QModelIndex index)
          This function is reimplemented for internal reasons.
 boolean eventFilter(QObject object, QEvent event)
          Returns true if the given object is a valid QWidget and the given event is handled; otherwise returns false.
static QItemDelegate fromNativePointer(QNativePointer nativePointer)
          This function returns the QItemDelegate instance pointed to by nativePointer
 boolean hasClipping()
          Returns if the delegate should clip the paint events.
 QItemEditorFactory itemEditorFactory()
          Returns the editor factory used by the item delegate.
 void paint(QPainter painter, QStyleOptionViewItem option, QModelIndex index)
          Renders the delegate using the given painter and style option for the item specified by index.
protected  QRect rect(QStyleOptionViewItem option, QModelIndex index, int role)
          
 void setClipping(boolean clip)
          Sets if the delegate should clip the paint events to clip.
 void setEditorData(QWidget editor, QModelIndex index)
          Sets the data to be displayed and edited by the editor for the item specified by index.
 void setItemEditorFactory(QItemEditorFactory factory)
          Sets the editor factory to be used by the item delegate to be the factory specified.
 void setModelData(QWidget editor, QAbstractItemModel model, QModelIndex index)
          Sets the data for the specified model and item index from that supplied by the editor.
protected  QStyleOptionViewItem setOptions(QModelIndex index, QStyleOptionViewItem option)
          
 QSize sizeHint(QStyleOptionViewItem option, QModelIndex index)
          Returns the size needed by the delegate to display the item specified by index, taking into account the style information provided by option.
protected  QRect textRectangle(QPainter painter, QRect rect, QFont font, java.lang.String text)
          
 void updateEditorGeometry(QWidget editor, QStyleOptionViewItem option, QModelIndex index)
          Updates the editor for the item specified by index according to the style option given.
 
Methods inherited from class com.trolltech.qt.gui.QAbstractItemDelegate
helpEvent
 
Methods inherited from class com.trolltech.qt.core.QObject
blockSignals, childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, property, removeEventFilter, setObjectName, setParent, setProperty, signalsBlocked, startTimer, thread, timerEvent
 
Methods inherited from class com.trolltech.qt.QtJambiObject
dispose, disposed, finalize, reassignNativeResources, tr, tr, tr
 
Methods inherited from class com.trolltech.qt.QSignalEmitter
disconnect, disconnect, signalSender
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.trolltech.qt.QtJambiInterface
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership
 

Constructor Detail

QItemDelegate

public QItemDelegate()

Equivalent to QItemDelegate(0).


QItemDelegate

public QItemDelegate(QObject parent)

Constructs an item delegate with the given parent.

Method Detail

check

protected final QRect check(QStyleOptionViewItem option,
                            QRect bounding,
                            java.lang.Object variant)


decoration

protected final QPixmap decoration(QStyleOptionViewItem option,
                                   java.lang.Object variant)


drawBackground

protected final void drawBackground(QPainter painter,
                                    QStyleOptionViewItem option,
                                    QModelIndex index)

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


hasClipping

public final boolean hasClipping()

Returns 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 then the size of the item.


itemEditorFactory

public final QItemEditorFactory itemEditorFactory()

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

See Also:
setItemEditorFactory

rect

protected final QRect rect(QStyleOptionViewItem option,
                           QModelIndex index,
                           int role)


setClipping

public final void setClipping(boolean clip)

Sets if the delegate should clip the paint events to clip.

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 then the size of the item.

See Also:
hasClipping

setItemEditorFactory

public final void 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.

See Also:
itemEditorFactory

setOptions

protected final QStyleOptionViewItem setOptions(QModelIndex index,
                                                QStyleOptionViewItem option)


textRectangle

protected final QRect textRectangle(QPainter painter,
                                    QRect rect,
                                    QFont font,
                                    java.lang.String text)


createEditor

public QWidget createEditor(QWidget parent,
                            QStyleOptionViewItem option,
                            QModelIndex index)

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.

Overrides:
createEditor in class QAbstractItemDelegate
See Also:
QAbstractItemDelegate::createEditor

drawCheck

protected void drawCheck(QPainter painter,
                         QStyleOptionViewItem option,
                         QRect rect,
                         Qt.CheckState state)

Renders a check indicator within the rectangle specified by rect, using the given painter and style option, using the given state.


drawDecoration

protected void drawDecoration(QPainter painter,
                              QStyleOptionViewItem option,
                              QRect rect,
                              QPixmap pixmap)

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


drawDisplay

protected void drawDisplay(QPainter painter,
                           QStyleOptionViewItem option,
                           QRect rect,
                           java.lang.String text)

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


drawFocus

protected void drawFocus(QPainter painter,
                         QStyleOptionViewItem option,
                         QRect rect)

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


editorEvent

public boolean editorEvent(QEvent event,
                           QAbstractItemModel model,
                           QStyleOptionViewItem option,
                           QModelIndex index)

This function is reimplemented for internal reasons.

Overrides:
editorEvent in class QAbstractItemDelegate

eventFilter

public boolean eventFilter(QObject object,
                           QEvent event)

Returns true if the given object is a valid QWidget and the given event is handled; otherwise returns false. The following key press events are handled by default:

In the case of Tab, Backtab, Enter and Return key press events, the object's data is comitted to the model and the editor is closed. If the event is a Tab key press the view will open an editor on the next item in the view. Likewise, if the event is a 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 object is closed without committing its data.

Overrides:
eventFilter in class QObject
See Also:
commitData, closeEditor

paint

public void paint(QPainter painter,
                  QStyleOptionViewItem option,
                  QModelIndex index)

Renders the delegate using the given painter and style option for the item specified by 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.

Specified by:
paint in class QAbstractItemDelegate
See Also:
QStyle::State

setEditorData

public void setEditorData(QWidget editor,
                          QModelIndex index)

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

The default implementation uses the editor's user property to set values.

Overrides:
setEditorData in class QAbstractItemDelegate
See Also:
QMetaProperty::isUser

setModelData

public void setModelData(QWidget editor,
                         QAbstractItemModel model,
                         QModelIndex index)

Sets the data for the specified model and item index from that supplied by the editor.

The default implementation uses the editor's user property to get values.

Overrides:
setModelData in class QAbstractItemDelegate
See Also:
QMetaProperty::isUser

sizeHint

public QSize sizeHint(QStyleOptionViewItem option,
                      QModelIndex index)

Returns the size needed by the delegate to display the item specified by index, taking into account the style information provided by 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.

Specified by:
sizeHint in class QAbstractItemDelegate

updateEditorGeometry

public void updateEditorGeometry(QWidget editor,
                                 QStyleOptionViewItem option,
                                 QModelIndex index)

Updates the editor for the item specified by index according to the style option given.

Overrides:
updateEditorGeometry in class QAbstractItemDelegate

fromNativePointer

public static QItemDelegate fromNativePointer(QNativePointer nativePointer)
This function returns the QItemDelegate instance pointed to by nativePointer

Parameters:
nativePointer - the QNativePointer of which object should be returned.

Qt Jambi Home