# wxDataViewModel Class Reference
[wxDataViewCtrl Related Classes]

`#include <wx/dataview.h>`

Inheritance diagram for wxDataViewModel:
[legend]

## Detailed Description

wxDataViewModel is the base class for all data model to be displayed by a wxDataViewCtrl.

All other models derive from it and must implement its pure virtual functions in order to define a complete data model. In detail, you need to override wxDataViewModel::IsContainer, wxDataViewModel::GetParent, wxDataViewModel::GetChildren, wxDataViewModel::GetColumnCount, wxDataViewModel::GetColumnType and wxDataViewModel::GetValue in order to define the data model which acts as an interface between your actual data and the wxDataViewCtrl.

Note that wxDataViewModel does not define the position or index of any item in the control because different controls might display the same data differently. wxDataViewModel does provide a wxDataViewModel::Compare method which the wxDataViewCtrl may use to sort the data either in conjunction with a column header or without (see wxDataViewModel::HasDefaultCompare).

wxDataViewModel (as indeed the entire wxDataViewCtrl code) is using wxVariant to store data and its type in a generic way. wxVariant can be extended to contain almost any data without changes to the original class. To a certain extent, you can use (the somewhat more elegant) wxAny instead of wxVariant as there is code to convert between the two, but it is unclear what impact this will have on performance.

Since you will usually allow the wxDataViewCtrl to change your data through its graphical interface, you will also have to override wxDataViewModel::SetValue which the wxDataViewCtrl will call when a change to some data has been committed.

If the data represented by the model is changed by something else than its associated wxDataViewCtrl, the control has to be notified about the change. Depending on what happened you need to call one of the following methods:

- wxDataViewModel::ValueChanged,
- wxDataViewModel::ItemAdded,
- wxDataViewModel::ItemDeleted,
- wxDataViewModel::ItemChanged,
- wxDataViewModel::Cleared.

There are plural forms for notification of addition, change or removal of several item at once. See:

- wxDataViewModel::ItemsAdded,
- wxDataViewModel::ItemsDeleted,
- wxDataViewModel::ItemsChanged.

This class maintains a list of wxDataViewModelNotifier which link this class to the specific implementations on the supported platforms so that e.g. calling wxDataViewModel::ValueChanged on this model will just call wxDataViewModelNotifier::ValueChanged for each notifier that has been added. You can also add your own notifier in order to get informed about any changes to the data in the list model.

Currently wxWidgets provides the following models apart from the base model: wxDataViewIndexListModel, wxDataViewVirtualListModel, wxDataViewTreeStore, wxDataViewListStore.

Note that wxDataViewModel is reference counted, derives from wxRefCounter and cannot be deleted directly as it can be shared by several wxDataViewCtrls. This implies that you need to decrease the reference count after associating the model with a control like this:

            wxDataViewCtrl *musicCtrl = new wxDataViewCtrl( this, wxID_ANY );
            wxDataViewModel *musicModel = new MyMusicModel;
            m_musicCtrl->AssociateModel( musicModel );
            musicModel->DecRef();  // avoid memory leak !!

            // add columns now

A potentially better way to avoid memory leaks is to use wxObjectDataPtr

            wxObjectDataPtr<MyMusicModel> musicModel;

            wxDataViewCtrl *musicCtrl = new wxDataViewCtrl( this, wxID_ANY );
            musicModel = new MyMusicModel;
            m_musicCtrl->AssociateModel( musicModel.get() );

            // add columns now

##

Library: wxAdvanced

Category: wxDataViewCtrl Related Classes

## Public Member Functions
| wxDataViewModel ()
| Constructor.
void | AddNotifier (wxDataViewModelNotifier *notifier)
| Adds a wxDataViewModelNotifier to the model.
bool | ChangeValue (const wxVariant &variant, const wxDataViewItem &item, unsigned int col)
| Change the value of the given item and update the control to reflect it.
virtual bool | Cleared ()
| Called to inform the model that all data has been cleared.
virtual int | Compare (const wxDataViewItem &item1, const wxDataViewItem &item2, unsigned int column, bool ascending) const
| The compare function to be used by control.
virtual bool | GetAttr (const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr) const
| Override this to indicate that the item has special font attributes.
virtual bool | IsEnabled (const wxDataViewItem &item, unsigned int col) const
| Override this to indicate that the item should be disabled.
virtual unsigned int | GetChildren (const wxDataViewItem &item, wxDataViewItemArray &children) const =0
| Override this so the control can query the child items of an item.
virtual unsigned int | GetColumnCount () const =0
| Override this to indicate the number of columns in the model.
virtual wxString | GetColumnType (unsigned int col) const =0
| Override this to indicate what type of data is stored in the column specified by col.
virtual wxDataViewItem | GetParent (const wxDataViewItem &item) const =0
| Override this to indicate which wxDataViewItem representing the parent of item or an invalid wxDataViewItem if the root item is the parent item.
virtual void | GetValue (wxVariant &variant, const wxDataViewItem &item, unsigned int col) const =0
| Override this to indicate the value of item.
virtual bool | HasContainerColumns (const wxDataViewItem &item) const
| Override this method to indicate if a container item merely acts as a headline (or for categorisation) or if it also acts a normal item with entries for further columns.
virtual bool | HasDefaultCompare () const
| Override this to indicate that the model provides a default compare function that the control should use if no wxDataViewColumn has been chosen for sorting.
bool | HasValue (const wxDataViewItem &item, unsigned col) const
| Return true if there is a value in the given column of this item.
virtual bool | IsContainer (const wxDataViewItem &item) const =0
| Override this to indicate of item is a container, i.e.
bool | ItemAdded (const wxDataViewItem &parent, const wxDataViewItem &item)
| Call this to inform the model that an item has been added to the data.
bool | ItemChanged (const wxDataViewItem &item)
| Call this to inform the model that an item has changed.
bool | ItemDeleted (const wxDataViewItem &parent, const wxDataViewItem &item)
| Call this to inform the model that an item has been deleted from the data.
bool | ItemsAdded (const wxDataViewItem &parent, const wxDataViewItemArray &items)
| Call this to inform the model that several items have been added to the data.
bool | ItemsChanged (const wxDataViewItemArray &items)
| Call this to inform the model that several items have changed.
bool | ItemsDeleted (const wxDataViewItem &parent, const wxDataViewItemArray &items)
| Call this to inform the model that several items have been deleted.
void | RemoveNotifier (wxDataViewModelNotifier *notifier)
| Remove the notifier from the list of notifiers.
virtual void | Resort ()
| Call this to initiate a resort after the sort function has been changed.
virtual bool | SetValue (const wxVariant &variant, const wxDataViewItem &item, unsigned int col)=0
| This gets called in order to set a value in the data model.
virtual bool | ValueChanged (const wxDataViewItem &item, unsigned int col)
| Call this to inform this model that a value in the model has been changed.
## Protected Member Functions
virtual | ~wxDataViewModel ()
| Destructor.

List of all members.

## Constructor & Destructor Documentation

wxDataViewModel::wxDataViewModel | ( |  | ) |

Constructor.

virtual wxDataViewModel::~wxDataViewModel | ( |  | ) | `[protected, virtual]`

Destructor.

This should not be called directly. Use DecRef() instead.

## Member Function Documentation

void wxDataViewModel::AddNotifier | ( | wxDataViewModelNotifier * | notifier | ) |

Adds a wxDataViewModelNotifier to the model.

bool wxDataViewModel::ChangeValue | ( | const wxVariant & | variant,
|  | const wxDataViewItem & | item,
|  | unsigned int | col |
| ) |  |  |

Change the value of the given item and update the control to reflect it.

This function simply calls SetValue() and, if it succeeded, ValueChanged().

Since:
  2.9.1

Parameters:

  | variant | The new value.
  | item | The item (row) to update.
  | col | The column to update.

Returns:
  true if both SetValue() and ValueChanged() returned true.

virtual bool wxDataViewModel::Cleared | ( |  | ) | `[virtual]`

Called to inform the model that all data has been cleared.

The control will reread the data from the model again.

virtual int wxDataViewModel::Compare | ( | const wxDataViewItem & | item1,
|  | const wxDataViewItem & | item2,
|  | unsigned int | column,
|  | bool | ascending |
| ) |  |  | const`[virtual]`

The compare function to be used by control.

The default compare function sorts by container and other items separately and in ascending order. Override this for a different sorting behaviour.

See also:
  HasDefaultCompare().

virtual bool wxDataViewModel::GetAttr | ( | const wxDataViewItem & | item,
|  | unsigned int | col,
|  | wxDataViewItemAttr & | attr |
| ) |  |  | const`[virtual]`

Override this to indicate that the item has special font attributes.

This only affects the wxDataViewTextRendererText renderer.

The base class version always simply returns false.

See also:
  wxDataViewItemAttr.

Parameters:

  | item | The item for which the attribute is requested.
  | col | The column of the item for which the attribute is requested.
  | attr | The attribute to be filled in if the function returns true.

Returns:
  true if this item has an attribute or false otherwise.

virtual unsigned int wxDataViewModel::GetChildren | ( | const wxDataViewItem & | item,
|  | wxDataViewItemArray & | children |
| ) |  |  | const`[pure virtual]`

Override this so the control can query the child items of an item.

Returns the number of items.

virtual unsigned int wxDataViewModel::GetColumnCount | ( |  | ) | const`[pure virtual]`

Override this to indicate the number of columns in the model.

Implemented in wxDataViewListStore.

virtual wxString wxDataViewModel::GetColumnType | ( | unsigned int | col | ) | const`[pure virtual]`

Override this to indicate what type of data is stored in the column specified by col.

This should return a string indicating the type of data as reported by wxVariant.

Implemented in wxDataViewListStore.

virtual wxDataViewItem wxDataViewModel::GetParent | ( | const wxDataViewItem & | item | ) | const`[pure virtual]`

Override this to indicate which wxDataViewItem representing the parent of item or an invalid wxDataViewItem if the root item is the parent item.

virtual void wxDataViewModel::GetValue | ( | wxVariant & | variant,
|  | const wxDataViewItem & | item,
|  | unsigned int | col |
| ) |  |  | const`[pure virtual]`

Override this to indicate the value of item.

A wxVariant is used to store the data.

virtual bool wxDataViewModel::HasContainerColumns | ( | const wxDataViewItem & | item | ) | const`[virtual]`

Override this method to indicate if a container item merely acts as a headline (or for categorisation) or if it also acts a normal item with entries for further columns.

By default returns false.

virtual bool wxDataViewModel::HasDefaultCompare | ( |  | ) | const`[virtual]`

Override this to indicate that the model provides a default compare function that the control should use if no wxDataViewColumn has been chosen for sorting.

Usually, the user clicks on a column header for sorting, the data will be sorted alphanumerically.

If any other order (e.g. by index or order of appearance) is required, then this should be used. See wxDataViewIndexListModel for a model which makes use of this.

bool wxDataViewModel::HasValue | ( | const wxDataViewItem & | item,
|  | unsigned | col |
| ) |  |  | const

Return true if there is a value in the given column of this item.

All normal items have values in all columns but the container items only show their label in the first column (col == 0) by default (but see HasContainerColumns()). So this function always returns true for the first column while for the other ones it returns true only if the item is not a container or HasContainerColumns() was overridden to return true for it.

Since:
  2.9.1

virtual bool wxDataViewModel::IsContainer | ( | const wxDataViewItem & | item | ) | const`[pure virtual]`

Override this to indicate of item is a container, i.e.

if it can have child items.

virtual bool wxDataViewModel::IsEnabled | ( | const wxDataViewItem & | item,
|  | unsigned int | col |
| ) |  |  | const`[virtual]`

Override this to indicate that the item should be disabled.

Disabled items are displayed differently (e.g. grayed out) and cannot be interacted with.

The base class version always returns true, thus making all items enabled by default.

Parameters:

  | item | The item whose enabled status is requested.
  | col | The column of the item whose enabled status is requested.

Returns:
  true if this item should be enabled, false otherwise.

Note:
  Currently disabling items is fully implemented only for the native control implementation in wxOSX/Cocoa and wxGTK. This feature is only partially supported in the generic version (used by wxMSW) and not supported by the wxOSX/Carbon implementation.

Since:
  2.9.2

bool wxDataViewModel::ItemAdded | ( | const wxDataViewItem & | parent,
|  | const wxDataViewItem & | item |
| ) |  |  |

Call this to inform the model that an item has been added to the data.

bool wxDataViewModel::ItemChanged | ( | const wxDataViewItem & | item | ) |

Call this to inform the model that an item has changed.

This will eventually emit a `wxEVT_DATAVIEW_ITEM_VALUE_CHANGED` event (in which the column fields will not be set) to the user.

bool wxDataViewModel::ItemDeleted | ( | const wxDataViewItem & | parent,
|  | const wxDataViewItem & | item |
| ) |  |  |

Call this to inform the model that an item has been deleted from the data.

bool wxDataViewModel::ItemsAdded | ( | const wxDataViewItem & | parent,
|  | const wxDataViewItemArray & | items |
| ) |  |  |

Call this to inform the model that several items have been added to the data.

bool wxDataViewModel::ItemsChanged | ( | const wxDataViewItemArray & | items | ) |

Call this to inform the model that several items have changed.

This will eventually emit `wxEVT_DATAVIEW_ITEM_VALUE_CHANGED` events (in which the column fields will not be set) to the user.

bool wxDataViewModel::ItemsDeleted | ( | const wxDataViewItem & | parent,
|  | const wxDataViewItemArray & | items |
| ) |  |  |

Call this to inform the model that several items have been deleted.

void wxDataViewModel::RemoveNotifier | ( | wxDataViewModelNotifier * | notifier | ) |

Remove the notifier from the list of notifiers.

virtual void wxDataViewModel::Resort | ( |  | ) | `[virtual]`

Call this to initiate a resort after the sort function has been changed.

virtual bool wxDataViewModel::SetValue | ( | const wxVariant & | variant,
|  | const wxDataViewItem & | item,
|  | unsigned int | col |
| ) |  |  | `[pure virtual]`

This gets called in order to set a value in the data model.

The most common scenario is that the wxDataViewCtrl calls this method after the user changed some data in the view.

This is the function you need to override in your derived class but if you want to call it, ChangeValue() is usually more convenient as otherwise you need to manually call ValueChanged() to update the control itself.

virtual bool wxDataViewModel::ValueChanged | ( | const wxDataViewItem & | item,
|  | unsigned int | col |
| ) |  |  | `[virtual]`

Call this to inform this model that a value in the model has been changed.

This is also called from wxDataViewCtrl's internal editing code, e.g. when editing a text field in the control.

This will eventually emit a `wxEVT_DATAVIEW_ITEM_VALUE_CHANGED` event to the user.

|  | [ top ]