Wt: Wt::WItemDelegate Class Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | List of all members
Wt::WItemDelegate Class Reference
Model/view system

Standard delegate class for rendering a view item. More...

`#include <Wt/WItemDelegate.h>`

Inheritance diagram for Wt::WItemDelegate:
[legend]

## Public Member Functions
| WItemDelegate ()
| Create an item delegate.
virtual std::unique_ptr< WWidget > | update (WWidget *widget, const WModelIndex &index, WFlags< ViewItemRenderFlag > flags) override
| Creates or updates a widget that renders an item.
virtual void | updateModelIndex (WWidget *widget, const WModelIndex &index) override
| Updates the model index of a widget.
void | setTextFormat (const WString &format)
| Sets the text format string.
const WString & | textFormat () const
| Returns the text format string.
virtual void | setModelData (const cpp17::any &editState, WAbstractItemModel *model, const WModelIndex &index) const override
| Saves the edited data to the model.
virtual cpp17::any | editState (WWidget *editor, const WModelIndex &index) const override
| Returns the current edit state.
virtual void | setEditState (WWidget *editor, const WModelIndex &index, const cpp17::any &value) const override
| Sets the editor data from the editor state.
Public Member Functions inherited from Wt::WAbstractItemDelegate
| WAbstractItemDelegate ()
| Constructor.
virtual | ~WAbstractItemDelegate ()
| Destructor.
virtual ValidationState | validate (const WModelIndex &index, const cpp17::any &editState) const
| Returns whether the edited value is valid.
Signal< WWidget *, bool > & | closeEditor ()
| Signal which indicates that an editor needs to be closed.
const Signal< WWidget *, bool > & | closeEditor () const
| Signal which indicates that an editor needs to be closed.
Public Member Functions inherited from Wt::WObject
void | addChild (std::unique_ptr< WObject > child)
| Add a child WObject whose lifetime is determined by this WObject.
template<typename Child >
Child * | addChild (std::unique_ptr< Child > child)
| Add a child WObject, returning a raw pointer.
std::unique_ptr< WObject > | removeChild (WObject *child)
| Remove a child WObject, so its lifetime is no longer determined by this WObject.
template<typename Child >
std::unique_ptr< Child > | removeChild (Child *child)
| Remove a child WObject, so its lifetime is no longer determined by this WObject.
virtual const std::string | id () const
| Returns the (unique) identifier for this object.
virtual void | setObjectName (const std::string &name)
| Sets an object name.
virtual std::string | objectName () const
| Returns the object name.
void | resetLearnedSlots ()
| Resets learned stateless slot implementations.
template<class T >
void | resetLearnedSlot (void(T::*method)())
| Resets a learned stateless slot implementation.
template<class T >
WStatelessSlot * | implementStateless (void(T::*method)())
| Declares a slot to be stateless and learn client-side behaviour on first invocation.
template<class T >
WStatelessSlot * | implementStateless (void(T::*method)(), void(T::*undoMethod)())
| Declares a slot to be stateless and learn client-side behaviour in advance.
void | isNotStateless ()
| Marks the current function as not stateless.
template<class T >
WStatelessSlot * | implementJavaScript (void(T::*method)(), const std::string &jsCode)
| Provides a JavaScript implementation for a method.
Public Member Functions inherited from Wt::Core::observable
| observable () noexcept
| Default constructor.
virtual | ~observable ()
| Destructor.
template<typename... Args, typename C >
auto | bindSafe (void(C::*method)(Args...)) noexcept
| Protects a method call against object destruction.
template<typename... Args, typename C >
auto | bindSafe (void(C::*method)(Args...) const) const noexcept
| Protects a const method call against object destruction.
template<typename Function >
auto | bindSafe (const Function &function) noexcept
| Protects a function against object destruction.

## Protected Member Functions
virtual std::unique_ptr< WWidget > | createEditor (const WModelIndex &index, WFlags< ViewItemRenderFlag > flags) const
| Creates an editor for a data item.
Protected Member Functions inherited from Wt::WObject
virtual WStatelessSlot * | getStateless (Method method)
| On-demand stateless slot implementation.

## Additional Inherited Members
Public Types inherited from Wt::WObject
typedef void(WObject::*) | Method()
| Typedef for a WObject method without arguments.
Static Public Member Functions inherited from Wt::WObject

## Detailed Description

Standard delegate class for rendering a view item.

This class provides the standard implementation for rendering an item (as in a WAbstractItemView), and renders data provided by the standard data roles (see ItemDataRole). It also provides default editing support using a line edit.

You may provide special editing support for an item by specializing the widget and reimplement createEditor(), setModelData(), editState(), and setEditState().

## Member Function Documentation

## ◆ createEditor()

virtual std::unique_ptr< WWidget > Wt::WItemDelegate::createEditor | ( | const WModelIndex & | index,
|  | WFlags< ViewItemRenderFlag > | flags ) const
| protectedvirtual

Creates an editor for a data item.

The default implementation returns a WLineEdit which edits the item's Wt::ItemDataRole::Edit value.

You may reimplement this method to provide a suitable editor, or to attach a custom validator. In that case, you will probably also want to reimplement editState(), setEditState(), and setModelData().

The editor should not keep a reference to the model index (it does not need to since setModelData() will provide the proper model index to save the data to the model). Otherwise, because model indexes may shift because of row or column insertions, you should reimplement updateModelIndex().

As an example of how to provide a specialized editor, consider the default implementation, which returns a WLineEdit:

std::unique_ptr<Wt::WWidget> WItemDelegate::createEditor(const Wt::WModelIndex& index, WFlags<ViewItemRenderFlag> flags) const
{
auto result = std::make_unique<Wt::WContainerWidget>();
result->setSelectable(true);
auto lineEdit = std::make_unique<Wt::WLineEdit>();
lineEdit->setText(asString(index.data(ItemDataRole::Edit), textFormat_));
lineEdit->enterPressed().connect(std::bind(&WItemDelegate::doCloseEditor, this, result, true));
lineEdit->escapePressed().connect(std::bind(&WItemDelegate::doCloseEditor, this, result, false));
if (flags.test(ViewItemRenderFlag::Focused))
lineEdit->setFocus();
// We use a layout so that the line edit fills the entire cell.
result->setLayout(std::make_unique<WHBoxLayout>());
result->layout()->setContentsMargins(1, 1, 1, 1);
result->layout()->addWidget(std::move(lineEdit));
return result;
}
void WItemDelegate::doCloseEditor(Wt::WWidget *editor, bool save) const
{
closeEditor().emit(editor, save);
}
Wt::ItemDataRole::Edit
static constexpr const int Edit
Role for the edited value.
Definition WModelIndex.h:152
Wt::WAbstractItemDelegate::closeEditor
Signal< WWidget *, bool > & closeEditor()
Signal which indicates that an editor needs to be closed.
Definition WAbstractItemDelegate.h:177
Wt::WFlags
Utility class for type-safe combinations of enumeration flags.
Definition WFlags.h:31
Wt::WFlags::test
bool test(EnumType flag) const
Returns whether a flag is set.
Definition WFlags.h:197
Wt::WItemDelegate::createEditor
virtual std::unique_ptr< WWidget > createEditor(const WModelIndex &index, WFlags< ViewItemRenderFlag > flags) const
Creates an editor for a data item.
Wt::WModelIndex
A value class that describes an index to an item in a data model.
Definition WModelIndex.h:292
Wt::WModelIndex::data
cpp17::any data(ItemDataRole role=ItemDataRole::Display) const
Returns data in the model at this index.
Wt::WWidget
The abstract base class for a user-interface component.
Definition WWidget.h:66
Wt::asString
WString asString(const cpp17::any &v, const WString &formatString=WString())
Interprets a cpp17::any as a string value.
Wt::ViewItemRenderFlag::Focused
@ Focused
Render (the editor) focused

## ◆ editState()

virtual cpp17::any Wt::WItemDelegate::editState | ( | WWidget * | editor,
|  | const WModelIndex & | index ) const
| overridevirtual

Returns the current edit state.

The default implementation returns the current text in the line edit. You will need to reimplement this method for a custom editor.

As an example of how to deal with a specialized editor, consider the default implementation:

cpp17::any WItemDelegate::editState(Wt::WWidget *editor) const
{
Wt::WContainerWidget *w = dynamic_cast<Wt::WContainerWidget *>(editor);
Wt::WLineEdit *lineEdit = dynamic_cast<Wt::WLineEdit *>(w->widget(0));
return cpp17::any(lineEdit->text());
}
Wt::WContainerWidget
A widget that holds and manages child widgets.
Definition WContainerWidget.h:135
Wt::WContainerWidget::widget
virtual WWidget * widget(int index) const
Returns the widget at index
Wt::WItemDelegate::editState
virtual cpp17::any editState(WWidget *editor, const WModelIndex &index) const override
Returns the current edit state.
Wt::WLineEdit
A widget that provides a single line edit.
Definition WLineEdit.h:76
Wt::WLineEdit::text
const WString & text() const
Returns the current content.
Definition WLineEdit.h:115

See also
  createEditor(), setEditState(), setModelData()

Reimplemented from Wt::WAbstractItemDelegate.

## ◆ setEditState()

virtual void Wt::WItemDelegate::setEditState | ( | WWidget * | editor,
|  | const WModelIndex & | index,
|  | const cpp17::any & | value ) const
| overridevirtual

Sets the editor data from the editor state.

The default implementation resets the text in the line edit. You will need to reimplement this method if for a custom editor.

As an example of how to deal with a specialized editor, consider the default implementation:

void WItemDelegate::setEditState(Wt::WWidget *editor, const WModelIndex& index, const cpp17::any& value) const
{
Wt::WContainerWidget *w = dynamic_cast<Wt::WContainerWidget *>(editor);
Wt::WLineEdit *lineEdit = dynamic_cast<Wt::WLineEdit *>(w->widget(0));
lineEdit->setText(cpp17::any_cast<Wt::WString>(value));
}
Wt::WItemDelegate::setEditState
virtual void setEditState(WWidget *editor, const WModelIndex &index, const cpp17::any &value) const override
Sets the editor data from the editor state.
Wt::WLineEdit::setText
virtual void setText(const WString &text)
Sets the content of the line edit.

See also
  createEditor()

Reimplemented from Wt::WAbstractItemDelegate.

## ◆ setModelData()

virtual void Wt::WItemDelegate::setModelData | ( | const cpp17::any & | editState,
|  | WAbstractItemModel * | model,
|  | const WModelIndex & | index ) const
| overridevirtual

Saves the edited data to the model.

The default implementation saves the current edit value to the model. You will need to reimplement this method for a custom editor.

As an example of how to deal with a specialized editor, consider the default implementation:

void WItemDelegate::setModelData(const Wt::cpp17::any& editState,
Wt::WAbstractItemModel *model,
const Wt::WModelIndex& index) const
{
model->setData(index, editState, ItemDataRole::Edit);
}
Wt::WAbstractItemModel
An abstract model for use with Wt's view classes.
Definition WAbstractItemModel.h:175
Wt::WAbstractItemModel::setData
virtual bool setData(const WModelIndex &index, const cpp17::any &value, ItemDataRole role=ItemDataRole::Edit)
Sets data at the given model index.
Wt::WItemDelegate::setModelData
virtual void setModelData(const cpp17::any &editState, WAbstractItemModel *model, const WModelIndex &index) const override
Saves the edited data to the model.

See also
  createEditor(), editState()

Reimplemented from Wt::WAbstractItemDelegate.

## ◆ setTextFormat()

void Wt::WItemDelegate::setTextFormat | ( | const WString & | format | ) |

Sets the text format string.

The ItemDataRole::Display data is converted to a string using asString() by passing the given format.

The default value is "".

## ◆ textFormat()

const WString & Wt::WItemDelegate::textFormat | ( |  | ) | const

Returns the text format string.

See also
  setTextFormat()

## ◆ update()

virtual std::unique_ptr< WWidget > Wt::WItemDelegate::update | ( | WWidget * | widget,
|  | const WModelIndex & | index,
|  | WFlags< ViewItemRenderFlag > | flags )
| overridevirtual

Creates or updates a widget that renders an item.

The following properties of an item are rendered:

- text using the Wt::ItemDataRole::Display data, with the format specified by setTextFormat()
- a check box depending on the Wt::ItemFlag::UserCheckable flag and Wt::ItemDataRole::Checked data
- an anchor depending on the value of Wt::ItemDataRole::Link
- an icon depending on the value of Wt::ItemDataRole::Decoration
- a tooltip depending on the value of Wt::ItemDataRole::ToolTip
- a custom style class depending on the value of Wt::ItemDataRole::StyleClass

When the flags indicates Wt::ViewItemRenderFlag::Editing, then createEditor() is called to create a suitable editor for editing the item.

Implements Wt::WAbstractItemDelegate.

## ◆ updateModelIndex()

virtual void Wt::WItemDelegate::updateModelIndex | ( | WWidget * | widget,
|  | const WModelIndex & | index )
| overridevirtual

Updates the model index of a widget.

This method is invoked by the view when due to row/column insertions or removals, the index has shifted.

You should reimplement this method only if you are storing the model index in the `widget`, to update the stored model index.

The default implementation does nothing.

Reimplemented from Wt::WAbstractItemDelegate.