Wt::WAbstractItemDelegate Class Reference
[Model/view system]

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

#include <Wt/WAbstractItemDelegate>

Inheritance diagram for Wt::WAbstractItemDelegate:

Inheritance graph
[legend]

List of all members.

Public Member Functions

 WAbstractItemDelegate (WObject *parent=0)
 Constructor.
virtual ~WAbstractItemDelegate ()
 Destructor.
virtual WWidgetupdate (WWidget *widget, const WModelIndex &index, WFlags< ViewItemRenderFlag > flags)=0
 Creates or updates a widget that renders an item.
virtual void updateModelIndex (WWidget *widget, const WModelIndex &index)
 Updates the model index of a widget.


Detailed Description

Abstract delegate class for rendering a view item.

Rendering of an item in a WTreeView is delegated to an implementation of this delegate class. The default implementation used by WTreeView is WItemDelegate. To provide specialized rendering support, you can reimplement this class, and indicate to the treeview to use this delegate for rendering items.

As a delegate is used for rendering multiple items, the class should not keep state about one specific item.

An example of a delegate that always renders the text in a line-edit, and saves the modified value back to the (editable) model.

// A custom delegate that uses an EditItem
class EditItemDelegate : public WAbstractItemDelegate
{
  class EditItem;

public:
  EditItemDelegate(WObject *parent = 0)
    : WAbstractItemDelegate(parent)
  { }

  // Creates or updates an EditItem
  WWidget *update(WWidget *widget, const WModelIndex& index,
                  WFlags<ViewItemRenderFlag> flags)
  {
    EditItem *item;

    if (widget) {
      item = dynamic_cast<EditItem *>(widget);
    } else {
      item = new EditItem(index);
      widget = item;
    }

    boost::any text = index.data(DisplayRole);
    item->edit()->setText(asString(text));

    return widget;
  }

  // Updates the model index of the EditItem
  void updateModelIndex(WWidget *widget, const WModelIndex& index)
  {
    EditItem *item = dynamic_cast<EditItem *>(widget);
    item->setIndex(index);
  }

private:

  // A custom item widget that contains only a WLineEdit
  class EditItem : public WContainerWidget
  {
  public:
    EditItem(const WModelIndex& index)
      : index_(index)
    {
      edit_ = new WLineEdit(this);
      edit_->changed().connect(SLOT(this, EditItem::onEdit));

      setAttributeValue("style", "line-height: 1em;"); // Otherwise the line edit overflows the cell
      setSelectable(true);                             // WTreeView by default disables selection
    }

    // Updates the model index
    void setIndex(const WModelIndex& index) { index_ = index; }

    // Returns the editor
    WLineEdit *edit() const { return edit_; }

  private:
    WLineEdit  *edit_;
    WModelIndex index_;

    // Updates the model with the edited value
    void onEdit() {
      WAbstractItemModel *model
        = const_cast<WAbstractItemModel *>(index_.model());

      model->setData(index_, edit_->text());
    }
  };
};

See also:
WTreeView::setItemDelegate(), WTreeView::setItemDelegateForColumn()

Member Function Documentation

virtual WWidget* Wt::WAbstractItemDelegate::update ( WWidget widget,
const WModelIndex index,
WFlags< ViewItemRenderFlag flags 
) [pure virtual]

Creates or updates a widget that renders an item.

The item is specified by its model index, which also indicates the model. If an existing widget already renders the item, but needs to be updated, it is passed as the widget parameter. You may decide to create a new widget, in which case you are responsible to delete the previous widget if it is not reused.

When widget is 0, a new widget needs to be created.

The flags parameter indicates options for rendering the item.

Implemented in Wt::WItemDelegate.

void Wt::WAbstractItemDelegate::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, an index was modified for a widget.

You should reimplement this method if you are storing the model index (e.g. for editing purposes) in the widget, which you should update to the new index.

The default implementation does nothing.

Reimplemented in Wt::WItemDelegate.


Generated on Fri Mar 26 17:12:06 2010 for Wt by doxygen 1.5.6