CopperSpice API  1.9.1
QAbstractItemDelegate Class Referenceabstract

The QAbstractItemDelegate class is used to display and edit data items from a model. More...

Inheritance diagram for QAbstractItemDelegate:
QObject QItemDelegate QStyledItemDelegate QSqlRelationalDelegate

Public Types

enum  EndEditHint
 

Public Signals

void closeEditor (QWidget *editor, QAbstractItemDelegate::EndEditHint hint=NoHint)
 
void commitData (QWidget *editor)
 
void sizeHintChanged (const QModelIndex &index)
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Methods

 QAbstractItemDelegate (QObject *parent=nullptr)
 
virtual ~QAbstractItemDelegate ()
 
virtual QWidgetcreateEditor (QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
 
virtual void destroyEditor (QWidget *editor, const QModelIndex &index) const
 
virtual bool editorEvent (QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
 
virtual bool helpEvent (QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)
 
virtual void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const = 0
 
virtual void setEditorData (QWidget *editor, const QModelIndex &index) const
 
virtual void setModelData (QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
 
virtual QSize sizeHint (const QStyleOptionViewItem &option, const QModelIndex &index) const = 0
 
virtual void updateEditorGeometry (QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Static Public Methods

static QString elidedText (const QFontMetrics &fontMetrics, int width, Qt::TextElideMode mode, const QString &text)
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The QAbstractItemDelegate class is used to display and edit data items from a model. It 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.

To render an item in a custom way, you must implement paint() and sizeHint(). The QItemDelegate class provides default implementations for these functions. If you do not need custom rendering, subclass that class instead.

Example

The following example shows how to draw a progress bar. The WidgetDelegate class inherits from QStyledItemDelegate. The actual drawing occurs in paint() based on the call to drawControl(). A QStyleOptionProgressBar is initialized and then passed to the current QStyle to draw it.

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 createEditor() to provide an editor widget, setEditorData() to populate the editor with the data from the model and setModelData() so the delegate can update the model with data from the editor.

The second approach is to handle user events directly by reimplementing editorEvent().

void WidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() == 1) {
int progress = index.data().toInt();
QStyleOptionProgressBar progressBarOption;
progressBarOption.rect = option.rect;
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.progress = progress;
progressBarOption.text = QString::number(progress) + "%";
progressBarOption.textVisible = true;
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
} else {
QStyledItemDelegate::paint(painter, option, index);
}
}
See also
QItemDelegate, QStyledItemDelegate, QStyle, Model/View Architecture

Member Enumeration Documentation

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.

ConstantValueDescription
QAbstractItemDelegate::NoHint0There is no recommended action to be performed.

These hints let the delegate influence the behavior of the view:

ConstantValueDescription
QAbstractItemDelegate::EditNextItem1The view should use the delegate to open an editor on the next item in the view.
QAbstractItemDelegate::EditPreviousItem2The view should use the delegate to open an editor on the previous item in the view.

Custom views may interpret the concepts of next and previous differently.

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.

ConstantValueDescription
QAbstractItemDelegate::SubmitModelCache3If the model caches data, it should write out cached data to the underlying data store.
QAbstractItemDelegate::RevertModelCache4If the model caches data, it should discard cached data and replace it with data from the underlying data store.

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.

Constructor & Destructor Documentation

QAbstractItemDelegate::QAbstractItemDelegate ( QObject parent = nullptr)
explicit

Construct a new abstract item delegate with the given parent.

QAbstractItemDelegate::~QAbstractItemDelegate ( )
virtual

Destroys the abstract item delegate.

Method Documentation

void QAbstractItemDelegate::closeEditor ( QWidget editor,
QAbstractItemDelegate::EndEditHint  hint = NoHint 
)
signal

This signal is emitted when the user has finished editing an item.

The specified editor represents the widget which was used to modify the item.

The hint 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 EditNextItem is specified the view should use a delegate to open an editor on the next item in the model.

See also
EndEditHint
void QAbstractItemDelegate::commitData ( QWidget editor)
signal

This signal must be emitted when the editor widget has completed the edit process. The slot method is responsible for calling the setEditorData() method to save the data back into the model.

QWidget * QAbstractItemDelegate::createEditor ( QWidget parent,
const QStyleOptionViewItem option,
const QModelIndex index 
) const
virtual

Returns the editor widget which should be used for editing the data with the given index. The editor's parent widget is specified by parent and the item options by option. The base implementation returns a nullptr.

To use custom editing you will need to reimplement this method in a custom delegate class. The returned editor widget should set the Qt::StrongFocus flag, otherwise QMouseEvents received by the widget will propagate to the view.

See also
setModelData(), setEditorData()

Reimplemented in QItemDelegate::createEditor(), QStyledItemDelegate::createEditor(), QSqlRelationalDelegate::createEditor()

void QAbstractItemDelegate::destroyEditor ( QWidget editor,
const QModelIndex index 
) const
virtual

Called when the editor is no longer needed for editing the data item with the given index and should be destroyed. The default behavior is a call to deleteLater on the editor. It is possible e.g. to avoid this delete by reimplementing this method.

See also
createEditor()
bool QAbstractItemDelegate::editorEvent ( QEvent event,
QAbstractItemModel model,
const QStyleOptionViewItem option,
const QModelIndex index 
)
virtual

When editing of an item starts this method is called with the event that triggered the editing, the model, the index of the item, and the option used for rendering the item. Mouse events are sent to editorEvent() even if they do not 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.

The base implementation returns false (indicating that it has not handled the event).

Reimplemented in QItemDelegate::editorEvent(), QStyledItemDelegate::editorEvent()

QString QAbstractItemDelegate::elidedText ( const QFontMetrics fontMetrics,
int  width,
Qt::TextElideMode  mode,
const QString text 
)
deprecatedstatic
Deprecated:
Use QFontMetrics::elidedText() instead.

If you have code similar to the following rewrite as shown.

QFontMetrics fm = ...
// old version
QString str = QAbstractItemDelegate::elidedText(fm, width, mode, text);
// new version
QString str = fm.elidedText(text, mode, width);
bool QAbstractItemDelegate::helpEvent ( QHelpEvent event,
QAbstractItemView view,
const QStyleOptionViewItem option,
const QModelIndex index 
)
virtual

Whenever a help event occurs, this method is called with the given event, view, option, and the index that corresponds to the item where the event occurs. 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.

For QEvent::ToolTip and QEvent::WhatsThis events that were handled successfully, the relevant popup may be shown depending on the user's system configuration.

See also
QHelpEvent
void QAbstractItemDelegate::paint ( QPainter painter,
const QStyleOptionViewItem option,
const QModelIndex index 
) const
pure virtual

This pure abstract function must be reimplemented if you want to provide custom rendering. Use the painter and style option to render the item specified by the item index.

If you reimplement this method, also reimplement sizeHint().

Implemented in QItemDelegate::paint(), QStyledItemDelegate::paint()

void QAbstractItemDelegate::setEditorData ( QWidget editor,
const QModelIndex index 
) const
virtual

Sets the contents of the given editor to the data for the item at the given index. The index contains information about the model being used.

The base implementation does nothing. If you want custom editing you will need to reimplement this function.

See also
setModelData()

Reimplemented in QItemDelegate::setEditorData(), QStyledItemDelegate::setEditorData(), QSqlRelationalDelegate::setEditorData()

void QAbstractItemDelegate::setModelData ( QWidget editor,
QAbstractItemModel model,
const QModelIndex index 
) const
virtual

Sets the data for the item at the given index in the model to the contents of the given editor.

The base implementation does nothing. If you want custom editing you will need to reimplement this function.

See also
setEditorData()

Reimplemented in QItemDelegate::setModelData(), QStyledItemDelegate::setModelData(), QSqlRelationalDelegate::setModelData()

QSize QAbstractItemDelegate::sizeHint ( const QStyleOptionViewItem option,
const QModelIndex index 
) const
pure virtual

This pure abstract method must be reimplemented if you want to provide custom rendering. The options are specified by option and the model item by index.

If you reimplement this you must also reimplement paint().

Implemented in QItemDelegate::sizeHint(), QStyledItemDelegate::sizeHint()

void QAbstractItemDelegate::sizeHintChanged ( const QModelIndex index)
signal

This signal must be emitted when the sizeHint() of index changed. Views automatically connect to this signal and relayout items as necessary.

void QAbstractItemDelegate::updateEditorGeometry ( QWidget editor,
const QStyleOptionViewItem option,
const QModelIndex index 
) const
virtual

Updates the geometry of the editor for the item with the given index to match the rectangle specified in option. If the item has an internal layout, the editor will be laid out accordingly. The index contains information about the model being used.

The base implementation for this method does nothing. If you want custom editing you must reimplement this method.

Reimplemented in QItemDelegate::updateEditorGeometry(), QStyledItemDelegate::updateEditorGeometry()