Wt: Wt::WItemDelegate Class Reference

Wt 3.3.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Hierarchy
- Class Members

Public Member Functions | Protected Member Functions
Wt::WItemDelegate Class Reference
Model/view system

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

`#include <Wt/WItemDelegate>`

Inheritance diagram for Wt::WItemDelegate:
[legend]

List of all members.

## Public Member Functions
| WItemDelegate (WObject *parent=0)
| Create an item delegate.
virtual WWidget * | update (WWidget *widget, const WModelIndex &index, WFlags< ViewItemRenderFlag > flags)
| Creates or updates a widget that renders an item.
virtual void | updateModelIndex (WWidget *widget, const WModelIndex &index)
| 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 boost::any &editState, WAbstractItemModel *model, const WModelIndex &index) const
| Saves the edited data to the model.
virtual boost::any | editState (WWidget *editor) const
| Returns the current edit state.
virtual void | setEditState (WWidget *editor, const boost::any &value) const
| Sets the editor data from the editor state.
## Protected Member Functions
virtual WWidget * | createEditor (const WModelIndex &index, WFlags< ViewItemRenderFlag > flags) const
| Creates an editor for a data item.

## 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

WWidget * Wt::WItemDelegate::createEditor | ( | const WModelIndex & | index,
|  | WFlags< ViewItemRenderFlag > | flags
| ) |  | const`[protected, virtual]`

Creates an editor for a data item.

The default implementation returns a WLineEdit which edits the item's Wt::EditRole 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:

     Wt::WWidget *WItemDelegate::createEditor(const Wt::WModelIndex& index, WFlags<ViewItemRenderFlag> flags) const
     {
       Wt::WContainerWidget *result = new Wt::WContainerWidget();
       result->setSelectable(true);

       Wt::WLineEdit *lineEdit = new Wt::WLineEdit();
       lineEdit->setText(asString(index.data(EditRole), textFormat_));
       lineEdit->enterPressed().connect(boost::bind(&WItemDelegate::doCloseEditor, this, result, true));
       lineEdit->escapePressed().connect(boost::bind(&WItemDelegate::doCloseEditor, this, result, false));

       if (flags & RenderFocused)
         lineEdit->setFocus();

       // We use a layout so that the line edit fills the entire cell.
       result->setLayout(new WHBoxLayout());
       result->layout()->setContentsMargins(1, 1, 1, 1);
       result->layout()->addWidget(lineEdit);

       return result;
     }

     void WItemDelegate::doCloseEditor(Wt::WWidget *editor, bool save) const
     {
       closeEditor().emit(editor, save);
     }

boost::any Wt::WItemDelegate::editState | ( | WWidget * | editor | ) | const`[virtual]`

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:

     boost::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 boost::any(lineEdit->text());
     }

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

Reimplemented from Wt::WAbstractItemDelegate.

void Wt::WItemDelegate::setEditState | ( | WWidget * | editor,
|  | const boost::any & | value
| ) |  | const`[virtual]`

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 boost::any& value) const
     {
       Wt::WContainerWidget *w = dynamic_cast<Wt::WContainerWidget *>(editor);
       Wt::WLineEdit *lineEdit = dynamic_cast<Wt::WLineEdit *>(w->widget(0));

       lineEdit->setText(boost::any_cast<Wt::WString>(value));
     }

See also:
  createEditor()

Reimplemented from Wt::WAbstractItemDelegate.

void Wt::WItemDelegate::setModelData | ( | const boost::any & | editState,
|  | WAbstractItemModel * | model,
|  | const WModelIndex & | index
| ) |  | const`[virtual]`

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 boost::any& editState,
                                      Wt::WAbstractItemModel *model,
                                      const Wt::WModelIndex& index) const
     {
       model->setData(index, editState, EditRole);
     }

See also:
  createEditor(), editState()

Reimplemented from Wt::WAbstractItemDelegate.

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

Sets the text format string.

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

The default value is "".

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

Returns the text format string.

See also:
  setTextFormat()

WWidget * Wt::WItemDelegate::update | ( | WWidget * | widget,
|  | const WModelIndex & | index,
|  | WFlags< ViewItemRenderFlag > | flags
| ) |  | `[virtual]`

Creates or updates a widget that renders an item.

The following properties of an item are rendered:

- text using the Wt::DisplayRole data, with the format specified by setTextFormat()
- a check box depending on the Wt::ItemIsUserCheckable flag and Wt::CheckStateRole data
- an anchor depending on Wt::InternalPathRole or Wt::UrlRole values
- an icon depending on the value of Wt::DecorationRole
- a tooltip depending on the value of Wt::ToolTipRole
- a custom style class depending on the value of Wt::StyleClassRole

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

Implements Wt::WAbstractItemDelegate.

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

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.

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator