[][src]Struct qt_widgets::QAbstractItemDelegate

#[repr(C)]pub struct QAbstractItemDelegate { /* fields omitted */ }

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

C++ class: QAbstractItemDelegate.

C++ documentation:

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

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.

The QAbstractItemDelegate class is one of the Model/View Classes and is part of Qt's model/view framework.

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.

We give an example of drawing a progress bar in items; in our case for a package management program.

We create the WidgetDelegate class, which inherits from QStyledItemDelegate. We do the drawing in the paint() function:

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);

Notice that we use a QStyleOptionProgressBar and initialize its members. We can then use 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 that the delegate can update the model with data from the editor.

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

Methods

impl QAbstractItemDelegate[src]

pub fn commit_data(&self) -> Signal<(*mut QWidget,)>[src]

This signal must be emitted when the editor widget has completed editing the data, and wants to write it back into the model.

Returns a built-in Qt signal QAbstractItemDelegate::commitData that can be passed to qt_core::Signal::connect.

C++ documentation:

This signal must be emitted when the editor widget has completed editing the data, and wants to write it back into the model.

pub fn close_editor(&self) -> Signal<(*mut QWidget, EndEditHint)>[src]

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

Returns a built-in Qt signal QAbstractItemDelegate::closeEditor that can be passed to qt_core::Signal::connect.

C++ documentation:

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

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.

pub fn size_hint_changed(&self) -> Signal<(*const QModelIndex,)>[src]

This signal must be emitted when the sizeHint() of index changed.

Returns a built-in Qt signal QAbstractItemDelegate::sizeHintChanged that can be passed to qt_core::Signal::connect.

C++ documentation:

This signal must be emitted when the sizeHint() of index changed.

Views automatically connect to this signal and relayout items as necessary.

This function was introduced in Qt 4.4.

pub unsafe fn create_editor(
    &self,
    parent: impl CastInto<Ptr<QWidget>>,
    option: impl CastInto<Ref<QStyleOptionViewItem>>,
    index: impl CastInto<Ref<QModelIndex>>
) -> QPtr<QWidget>
[src]

Returns the editor to be used for editing the data item with the given index. Note that the index contains information about the model being used. The editor's parent widget is specified by parent, and the item options by option.

Calls C++ function: virtual QWidget* QAbstractItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const.

C++ documentation:

Returns the editor to be used for editing the data item with the given index. Note that the index contains information about the model being used. The editor's parent widget is specified by parent, and the item options by option.

The base implementation returns 0. If you want custom editing you will need to reimplement this function.

The returned editor widget should have Qt::StrongFocus; otherwise, QMouseEvents 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 setAutoFillBackground()).

See also destroyEditor(), setModelData(), and setEditorData().

pub unsafe fn destroy_editor(
    &self,
    editor: impl CastInto<Ptr<QWidget>>,
    index: impl CastInto<Ref<QModelIndex>>
)
[src]

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

Calls C++ function: virtual void QAbstractItemDelegate::destroyEditor(QWidget* editor, const QModelIndex& index) const.

C++ documentation:

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

This function was introduced in Qt 5.0.

See also createEditor().

pub unsafe fn editor_event(
    &self,
    event: impl CastInto<Ptr<QEvent>>,
    model: impl CastInto<Ptr<QAbstractItemModel>>,
    option: impl CastInto<Ref<QStyleOptionViewItem>>,
    index: impl CastInto<Ref<QModelIndex>>
) -> bool
[src]

When editing of an item starts, this function is called with the event that triggered the editing, the model, the index of the item, and the option used for rendering the item.

Calls C++ function: virtual bool QAbstractItemDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index).

C++ documentation:

When editing of an item starts, this function 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 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.

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

pub unsafe fn elided_text(
    font_metrics: impl CastInto<Ref<QFontMetrics>>,
    width: c_int,
    mode: TextElideMode,
    text: impl CastInto<Ref<QString>>
) -> CppBox<QString>
[src]

Use QFontMetrics::elidedText() instead.

Calls C++ function: static QString QAbstractItemDelegate::elidedText(const QFontMetrics& fontMetrics, int width, Qt::TextElideMode mode, const QString& text).

C++ documentation:

Use QFontMetrics::elidedText() instead.

For example, if you have code like

QFontMetrics fm = ... QString str = QAbstractItemDelegate::elidedText(fm, width, mode, text);

you can rewrite it as

QFontMetrics fm = ... QString str = fm.elidedText(text, mode, width);

pub unsafe fn help_event(
    &self,
    event: impl CastInto<Ptr<QHelpEvent>>,
    view: impl CastInto<Ptr<QAbstractItemView>>,
    option: impl CastInto<Ref<QStyleOptionViewItem>>,
    index: impl CastInto<Ref<QModelIndex>>
) -> bool
[src]

Whenever a help event occurs, this function is called with the event view option and the index that corresponds to the item where the event occurs.

Calls C++ function: virtual bool QAbstractItemDelegate::helpEvent(QHelpEvent* event, QAbstractItemView* view, const QStyleOptionViewItem& option, const QModelIndex& index).

C++ documentation:

Whenever a help event occurs, this function is called with the 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.

This function was introduced in Qt 4.3.

See also QHelpEvent.

pub unsafe fn meta_object(&self) -> Ptr<QMetaObject>[src]

Calls C++ function: virtual const QMetaObject* QAbstractItemDelegate::metaObject() const.

pub unsafe fn paint(
    &self,
    painter: impl CastInto<Ptr<QPainter>>,
    option: impl CastInto<Ref<QStyleOptionViewItem>>,
    index: impl CastInto<Ref<QModelIndex>>
)
[src]

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.

Calls C++ function: pure virtual void QAbstractItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const.

C++ documentation:

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 you must also reimplement sizeHint().

pub unsafe fn painting_roles(&self) -> CppBox<QVectorOfInt>[src]

Calls C++ function: virtual QVector<int> QAbstractItemDelegate::paintingRoles() const.

pub unsafe fn qt_metacall(
    &self,
    arg1: Call,
    arg2: c_int,
    arg3: *mut *mut c_void
) -> c_int
[src]

Calls C++ function: virtual int QAbstractItemDelegate::qt_metacall(QMetaObject::Call arg1, int arg2, void** arg3).

pub unsafe fn qt_metacast(&self, arg1: *const c_char) -> *mut c_void[src]

Calls C++ function: virtual void* QAbstractItemDelegate::qt_metacast(const char* arg1).

pub unsafe fn set_editor_data(
    &self,
    editor: impl CastInto<Ptr<QWidget>>,
    index: impl CastInto<Ref<QModelIndex>>
)
[src]

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

Calls C++ function: virtual void QAbstractItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const.

C++ documentation:

Sets the contents of the given editor to the data for the item at the given index. Note that 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().

pub unsafe fn set_model_data(
    &self,
    editor: impl CastInto<Ptr<QWidget>>,
    model: impl CastInto<Ptr<QAbstractItemModel>>,
    index: impl CastInto<Ref<QModelIndex>>
)
[src]

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

Calls C++ function: virtual void QAbstractItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const.

C++ documentation:

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().

pub unsafe fn size_hint(
    &self,
    option: impl CastInto<Ref<QStyleOptionViewItem>>,
    index: impl CastInto<Ref<QModelIndex>>
) -> CppBox<QSize>
[src]

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

Calls C++ function: pure virtual QSize QAbstractItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const.

C++ documentation:

This pure abstract function 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().

pub unsafe fn static_meta_object() -> Ref<QMetaObject>[src]

Returns a reference to the staticMetaObject field.

pub unsafe fn tr(
    s: *const c_char,
    c: *const c_char,
    n: c_int
) -> CppBox<QString>
[src]

Calls C++ function: static QString QAbstractItemDelegate::tr(const char* s, const char* c, int n).

pub unsafe fn tr_utf8(
    s: *const c_char,
    c: *const c_char,
    n: c_int
) -> CppBox<QString>
[src]

Calls C++ function: static QString QAbstractItemDelegate::trUtf8(const char* s, const char* c, int n).

pub unsafe fn update_editor_geometry(
    &self,
    editor: impl CastInto<Ptr<QWidget>>,
    option: impl CastInto<Ref<QStyleOptionViewItem>>,
    index: impl CastInto<Ref<QModelIndex>>
)
[src]

Updates the geometry of the editor for the item with the given index, according to the rectangle specified in the option. 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.

Calls C++ function: virtual void QAbstractItemDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const.

C++ documentation:

Updates the geometry of the editor for the item with the given index, according to the rectangle specified in the option. 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.

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

Trait Implementations

impl CppDeletable for QAbstractItemDelegate[src]

unsafe fn delete(&self)[src]

Destroys the abstract item delegate.

Calls C++ function: virtual [destructor] void QAbstractItemDelegate::~QAbstractItemDelegate().

C++ documentation:

Destroys the abstract item delegate.

impl Deref for QAbstractItemDelegate[src]

type Target = QObject

The resulting type after dereferencing.

fn deref(&self) -> &QObject[src]

Calls C++ function: QObject* static_cast<QObject*>(QAbstractItemDelegate* ptr).

impl DynamicCast<QAbstractItemDelegate> for QObject[src]

unsafe fn dynamic_cast(ptr: Ptr<QObject>) -> Ptr<QAbstractItemDelegate>[src]

Calls C++ function: QAbstractItemDelegate* dynamic_cast<QAbstractItemDelegate*>(QObject* ptr).

impl DynamicCast<QItemDelegate> for QAbstractItemDelegate[src]

unsafe fn dynamic_cast(ptr: Ptr<QAbstractItemDelegate>) -> Ptr<QItemDelegate>[src]

Calls C++ function: QItemDelegate* dynamic_cast<QItemDelegate*>(QAbstractItemDelegate* ptr).

impl DynamicCast<QStyledItemDelegate> for QAbstractItemDelegate[src]

unsafe fn dynamic_cast(
    ptr: Ptr<QAbstractItemDelegate>
) -> Ptr<QStyledItemDelegate>
[src]

Calls C++ function: QStyledItemDelegate* dynamic_cast<QStyledItemDelegate*>(QAbstractItemDelegate* ptr).

impl StaticDowncast<QAbstractItemDelegate> for QObject[src]

unsafe fn static_downcast(ptr: Ptr<QObject>) -> Ptr<QAbstractItemDelegate>[src]

Calls C++ function: QAbstractItemDelegate* static_cast<QAbstractItemDelegate*>(QObject* ptr).

impl StaticDowncast<QItemDelegate> for QAbstractItemDelegate[src]

unsafe fn static_downcast(ptr: Ptr<QAbstractItemDelegate>) -> Ptr<QItemDelegate>[src]

Calls C++ function: QItemDelegate* static_cast<QItemDelegate*>(QAbstractItemDelegate* ptr).

impl StaticDowncast<QStyledItemDelegate> for QAbstractItemDelegate[src]

unsafe fn static_downcast(
    ptr: Ptr<QAbstractItemDelegate>
) -> Ptr<QStyledItemDelegate>
[src]

Calls C++ function: QStyledItemDelegate* static_cast<QStyledItemDelegate*>(QAbstractItemDelegate* ptr).

impl StaticUpcast<QAbstractItemDelegate> for QItemDelegate[src]

unsafe fn static_upcast(ptr: Ptr<QItemDelegate>) -> Ptr<QAbstractItemDelegate>[src]

Calls C++ function: QAbstractItemDelegate* static_cast<QAbstractItemDelegate*>(QItemDelegate* ptr).

impl StaticUpcast<QAbstractItemDelegate> for QStyledItemDelegate[src]

unsafe fn static_upcast(
    ptr: Ptr<QStyledItemDelegate>
) -> Ptr<QAbstractItemDelegate>
[src]

Calls C++ function: QAbstractItemDelegate* static_cast<QAbstractItemDelegate*>(QStyledItemDelegate* ptr).

impl StaticUpcast<QObject> for QAbstractItemDelegate[src]

unsafe fn static_upcast(ptr: Ptr<QAbstractItemDelegate>) -> Ptr<QObject>[src]

Calls C++ function: QObject* static_cast<QObject*>(QAbstractItemDelegate* ptr).

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T, U> CastInto<U> for T where
    U: CastFrom<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> StaticUpcast<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.