|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.core.QObject
com.trolltech.qt.gui.QAbstractItemDelegate
com.trolltech.qt.gui.QItemDelegate
public class QItemDelegate
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.
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 | QBrush |
Qt::BackgroundColorRole | QColor (obsolete; use Qt::BackgroundRole instead) |
Qt::CheckStateRole | Qt::CheckState |
Qt::DecorationRole | QIcon 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) |
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.
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.
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.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)
This method is used internally by Qt Jambi. |
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)
This method is used internally by Qt Jambi. |
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)
This method is used internally by Qt Jambi. |
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)
This method is used internally by Qt Jambi. |
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)
This method is used internally by Qt Jambi. |
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 |
---|
public QItemDelegate()
Equivalent to QItemDelegate(0).
public QItemDelegate(QObject parent)
Constructs an item delegate with the given parent.
Method Detail |
---|
protected final QRect check(QStyleOptionViewItem option, QRect bounding, java.lang.Object variant)
This method is used internally by Qt Jambi. Do not use it in your applications.
protected final QPixmap decoration(QStyleOptionViewItem option, java.lang.Object variant)
This method is used internally by Qt Jambi. Do not use it in your applications.
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.
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.
public final QItemEditorFactory itemEditorFactory()
Returns the editor factory used by the item delegate. If no editor factory is set, the function will return null.
protected final QRect rect(QStyleOptionViewItem option, QModelIndex index, int role)
This method is used internally by Qt Jambi. Do not use it in your applications.
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.
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.
protected final QStyleOptionViewItem setOptions(QModelIndex index, QStyleOptionViewItem option)
This method is used internally by Qt Jambi. Do not use it in your applications.
protected final QRect textRectangle(QPainter painter, QRect rect, QFont font, java.lang.String text)
This method is used internally by Qt Jambi. Do not use it in your applications.
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.
createEditor
in class QAbstractItemDelegate
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.
public boolean editorEvent(QEvent event, QAbstractItemModel model, QStyleOptionViewItem option, QModelIndex index)
This function is reimplemented for internal reasons.
editorEvent
in class QAbstractItemDelegate
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.
eventFilter
in class QObject
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.
paint
in class QAbstractItemDelegate
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.
setEditorData
in class QAbstractItemDelegate
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.
setModelData
in class QAbstractItemDelegate
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.
sizeHint
in class QAbstractItemDelegate
public void updateEditorGeometry(QWidget editor, QStyleOptionViewItem option, QModelIndex index)
Updates the editor for the item specified by index according to the style option given.
updateEditorGeometry
in class QAbstractItemDelegate
public static QItemDelegate fromNativePointer(QNativePointer nativePointer)
nativePointer
- the QNativePointer of which object should be returned.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |