Qt 4.0: QAbstractItemModel Class Reference

|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# QAbstractItemModel Class Reference
[QtCore module]

The QAbstractItemModel class provides the abstract interface for item model classes. More...

    #include <QAbstractItemModel>

Inherits QObject.

Inherited by QAbstractListModel, QAbstractTableModel, QDirModel, QProxyModel, and QStandardItemModel.

- List of all members, including inherited members

### Public Functions

- QAbstractItemModel ( QObject * parent = 0 )
- virtual ~QAbstractItemModel ()
- virtual QModelIndex buddy ( const QModelIndex & index ) const
- virtual bool canFetchMore ( const QModelIndex & parent ) const
- virtual int columnCount ( const QModelIndex & parent = QModelIndex() ) const = 0
- virtual QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const = 0
- virtual bool dropMimeData ( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent )
- virtual void fetchMore ( const QModelIndex & parent )
- virtual Qt::ItemFlags flags ( const QModelIndex & index ) const
- virtual bool hasChildren ( const QModelIndex & parent = QModelIndex() ) const
- bool hasIndex ( int row, int column, const QModelIndex & parent = QModelIndex() ) const
- virtual QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const
- virtual QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const = 0
- bool insertColumn ( int column, const QModelIndex & parent = QModelIndex() )
- virtual bool insertColumns ( int column, int count, const QModelIndex & parent = QModelIndex() )
- bool insertRow ( int row, const QModelIndex & parent = QModelIndex() )
- virtual bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() )
- virtual QMap<int, QVariant> itemData ( const QModelIndex & index ) const
- virtual QModelIndexList match ( const QModelIndex & start, int role, const QVariant & value, int hits = 1, Qt::MatchFlags flags = Qt::MatchFlags( Qt::MatchStartsWith | Qt::MatchWrap ) ) const
- virtual QMimeData * mimeData ( const QModelIndexList & indexes ) const
- virtual QStringList mimeTypes () const
- virtual QModelIndex parent ( const QModelIndex & index ) const = 0
- bool removeColumn ( int column, const QModelIndex & parent = QModelIndex() )
- virtual bool removeColumns ( int column, int count, const QModelIndex & parent = QModelIndex() )
- bool removeRow ( int row, const QModelIndex & parent = QModelIndex() )
- virtual bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() )
- virtual int rowCount ( const QModelIndex & parent = QModelIndex() ) const = 0
- virtual bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole )
- virtual bool setHeaderData ( int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole )
- virtual bool setItemData ( const QModelIndex & index, const QMap<int, QVariant> & roles )
- QModelIndex sibling ( int row, int column, const QModelIndex & index ) const
- virtual void sort ( int column, Qt::SortOrder order = Qt::AscendingOrder )
- virtual QSize span ( const QModelIndex & index ) const
- virtual Qt::DropActions supportedDropActions () const

- 28 public functions inherited from QObject

### Public Slots

- virtual void revert ()
- virtual bool submit ()

- 1 public slot inherited from QObject

### Signals

- void dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight )
- void headerDataChanged ( Qt::Orientation orientation, int first, int last )
- void layoutChanged ()

- 1 signal inherited from QObject

### Protected Functions

- void beginInsertColumns ( const QModelIndex & parent, int first, int last )
- void beginInsertRows ( const QModelIndex & parent, int first, int last )
- void beginRemoveColumns ( const QModelIndex & parent, int first, int last )
- void beginRemoveRows ( const QModelIndex & parent, int first, int last )
- void changePersistentIndex ( const QModelIndex & from, const QModelIndex & to )
- QModelIndex createIndex ( int row, int column, void * ptr = 0 ) const
- QModelIndex createIndex ( int row, int column, int id ) const
- void endInsertColumns ()
- void endInsertRows ()
- void endRemoveColumns ()
- void endRemoveRows ()
- void reset ()

- 7 protected functions inherited from QObject

### Additional Inherited Members

- 1 property inherited from QObject
- 4 static public members inherited from QObject

## Detailed Description

The QAbstractItemModel class provides the abstract interface for item model classes.

The QAbstractItemModel class defines the standard interface that item models must use to be able to interoperate with other components in the model/view architecture. It is not supposed to be instantiated directly. Instead, you should subclass it to create new models.

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

If you need a model to use with a QListView or a QTableView, you should consider subclassing QAbstractListModel or QAbstractTableModel instead of this class.

The underlying data model is exposed to views and delegates as a hierarchy of tables. If you don't make use of the hierarchy, then the model is a simple table of rows and columns. Each item has a unique index specified by a QModelIndex.

Every item of data that can be accessed via a model has an associated model index that is obtained using the index() function. Each index may have a sibling() index; child items have a parent() index.

Each item has a number of data elements associated with it, and each of these can be retrieved by specifying a role (see Qt::ItemDataRole) to the model's data() function. Data for all available roles can be obtained at the same time using the itemData() function.

Data for each role is set using a particular Qt::ItemDataRole. Data for individual roles are set individually with setData(), or they can be set for all roles with setItemData().

Items can be queried with flags() (see Qt::ItemFlag) to see if they can be selected, dragged, or manipulated in other ways.

If an item has child objects, hasChildren() returns true for the corresponding index.

The model has a rowCount() and a columnCount() for each level of the hierarchy. Rows and columns can be inserted and removed with insertRows(), insertColumns(), removeRows(), and removeColumns().

The model emits signals to indicate changes. For example, dataChanged() is emitted whenever items of data made available by the model are changed. Changes to the headers supplied by the model cause headerDataChanged() to be emitted. If the structure of the underlying data changes, the model can emit layoutChanged() to indicate to any attached views that they should redisplay any items shown, taking the new structure into account.

The items available through the model can be searched for particular data using the match() function.

If the model is sortable, it can be sorted with sort().

### Subclassing

When subclassing QAbstractItemModel, at the very least you must implement index(), parent(), rowCount(), columnCount(), hasChildren(), and data(). To enable editing in your model, you must also implement setData(), and reimplement flags() to ensure that `ItemIsEditable` is returned.

You can also reimplement headerData() and setHeaderData() to control the way the headers for your model are presented.

Custom models need to create model indexes for other components to use. To do this, call createIndex() with suitable row and column numbers for the item, and supply a unique identifier for the item, either as a pointer or as an integer value. Custom models typically use these unique identifiers in other reimplemented functions to retrieve item data and access information about the item's parents and children. See the Simple Tree Model example for more information about unique identifiers.

Models that provide interfaces to resizable data structures can provide implementations of insertRows(), removeRows(), insertColumns(), and removeColumns(). When implementing these functions, it is important to notify any connected views about changes to the model's dimensions both before and after they occur:

- An insertRows() implementation must call beginInsertRows() before inserting new rows into the data structure, and it must call endInsertRows() immediately afterwards.
- An insertColumns() implementation must call beginInsertColumns() before inserting new columns into the data structure, and it must call endInsertColumns() immediately afterwards.
- A removeRows() implementation must call beginRemoveRows() before the rows are removed from the data structure, and it must call endRemoveRows() immediately afterwards.
- A removeColumns() implementation must call beginRemoveColumns() before the columns are removed from the data structure, and it must call endRemoveColumns() immediately afterwards.

The signals that these functions emit give attached components the chance to take action before any data becomes unavailable. The encapsulation of the insert and remove operations with these begin and end functions also enable the model to manage persistent model indexes correctly. If you want selections to be handled properly, you must ensure that you call these functions.

See also Model/View Programming, QModelIndex, and QAbstractItemView.

## Member Function Documentation

### QAbstractItemModel::QAbstractItemModel ( QObject * parent = 0 )

Constructs an abstract item model with the given parent.

### QAbstractItemModel::~QAbstractItemModel () `[virtual]`

Destroys the abstract item model.

### void QAbstractItemModel::beginInsertColumns ( const QModelIndex & parent, int first, int last ) `[protected]`

Begins a column insertion operation.

When reimplementing insertColumns() in a subclass, you must call this function before inserting data into the model's underlying data store.

The parent index corresponds to the parent into which the new columns are inserted; first and last are the column numbers of the new columns to be inserted.

See also endInsertColumns().

### void QAbstractItemModel::beginInsertRows ( const QModelIndex & parent, int first, int last ) `[protected]`

Begins a row insertion operation.

When reimplementing insertRows() in a subclass, you must call this function before inserting data into the model's underlying data store.

The parent index corresponds to the parent into which the new rows are inserted; first and last are the row numbers of the new rows to be inserted.

See also endInsertRows().

### void QAbstractItemModel::beginRemoveColumns ( const QModelIndex & parent, int first, int last ) `[protected]`

Begins a column removal operation.

When reimplementing removeColumns() in a subclass, you must call this function before removing data from the model's underlying data store.

The parent index corresponds to the parent from which the new columns are removed; first and last are the column numbers of the columns to be removed.

See also endRemoveColumns().

### void QAbstractItemModel::beginRemoveRows ( const QModelIndex & parent, int first, int last ) `[protected]`

Begins a row removal operation.

When reimplementing removeRows() in a subclass, you must call this function before removing data from the model's underlying data store.

The parent index corresponds to the parent from which the new rows are removed; first and last are the row numbers of the rows to be removed.

See also endRemoveRows().

### QModelIndex QAbstractItemModel::buddy ( const QModelIndex & index ) const `[virtual]`

Returns a model index for the buddy of the item represented by index. When the user wants to edit an item, the view will call this function to check whether another item in the model should be edited instead, and construct a delegate using the model index returned by the buddy item.

In the default implementation each item is its own buddy.

### bool QAbstractItemModel::canFetchMore ( const QModelIndex & parent ) const `[virtual]`

Returns true if there is more data available for parent, otherwise false.

The default implementation always returns false.

See also fetchMore().

### void QAbstractItemModel::changePersistentIndex ( const QModelIndex & from, const QModelIndex & to ) `[protected]`

Changes the QPersistentModelIndex that is equal to the given from model index to the given to model index.

If no persistent model index equal to the given from model index was found, nothing is changed.

### int QAbstractItemModel::columnCount ( const QModelIndex & parent = QModelIndex() ) const `[pure virtual]`

Returns the number of columns for the given parent.

### QModelIndex QAbstractItemModel::createIndex ( int row, int column, void * ptr = 0 ) const `[protected]`

Creates a model index for the given row and column with the internal pointer ptr.

This function provides a consistent interface that model subclasses must use to create model indexes.

### QModelIndex QAbstractItemModel::createIndex ( int row, int column, int id ) const `[protected]`

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Creates a model index for the given row and column with the internal identifier id.

This function provides a consistent interface that model subclasses must use to create model indexes.

### QVariant QAbstractItemModel::data ( const QModelIndex & index, int role = Qt::DisplayRole ) const `[pure virtual]`

Returns the data stored under the given role for the item referred to by the index.

See also Qt::ItemDataRole, setData(), and headerData().

### void QAbstractItemModel::dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight ) `[signal]`

This signal is emitted whenever the data in an existing item changes. The affected items are those between topLeft and bottomRight inclusive.

See also headerDataChanged(), setData(), and layoutChanged().

### bool QAbstractItemModel::dropMimeData ( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ) `[virtual]`

Handles the data supplied by a drag and drop operation that ended with the given action over the row in the model specified by the row, column, and the parent index.

See also supportedDropActions().

### void QAbstractItemModel::endInsertColumns () `[protected]`

Ends a column insertion operation.

When reimplementing insertColumns() in a subclass, you must call this function after inserting data into the model's underlying data store.

See also beginInsertColumns().

### void QAbstractItemModel::endInsertRows () `[protected]`

Ends a row insertion operation.

When reimplementing insertRows() in a subclass, you must call this function after inserting data into the model's underlying data store.

See also beginInsertRows().

### void QAbstractItemModel::endRemoveColumns () `[protected]`

Ends a column removal operation.

When reimplementing removeColumns() in a subclass, you must call this function after removing data from the model's underlying data store.

See also beginRemoveColumns().

### void QAbstractItemModel::endRemoveRows () `[protected]`

Ends a row removal operation.

When reimplementing removeRows() in a subclass, you must call this function after removing data from the model's underlying data store.

See also beginRemoveRows().

### void QAbstractItemModel::fetchMore ( const QModelIndex & parent ) `[virtual]`

Fetches any available data for the items with the parent specified by the parent index.

Reimplement this if you have incremental data.

The default implementation does nothing.

See also canFetchMore().

### Qt::ItemFlags QAbstractItemModel::flags ( const QModelIndex & index ) const `[virtual]`

Returns the item flags for the given index.

The base class implementation returns a combination of flags that enables the item (`ItemIsEnabled`) and allows it to be selected (`ItemIsSelectable`).

See also Qt::ItemFlags.

### bool QAbstractItemModel::hasChildren ( const QModelIndex & parent = QModelIndex() ) const `[virtual]`

Returns true if parent has any children; otherwise returns false.

See also parent() and index().

### bool QAbstractItemModel::hasIndex ( int row, int column, const QModelIndex & parent = QModelIndex() ) const

Returns true if the model returns a valid QModelIndex for row and column with parent, otherwise returns false.

### QVariant QAbstractItemModel::headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const `[virtual]`

Returns the data for the given role and section in the header with the specified orientation.

See also Qt::ItemDataRole and setHeaderData().

### void QAbstractItemModel::headerDataChanged ( Qt::Orientation orientation, int first, int last ) `[signal]`

This signal is emitted whenever a header is changed. The orientation indicates whether the horizontal or vertical header has changed. The sections in the header from the first to the last need to be updated.

See also headerData(), setHeaderData(), and dataChanged().

### QModelIndex QAbstractItemModel::index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const `[pure virtual]`

Returns the index of the item in the model specified by the given row, column and parent index.

### bool QAbstractItemModel::insertColumn ( int column, const QModelIndex & parent = QModelIndex() )

Inserts a single column before the given column in the child items of the parent specified. Returns true if the column is inserted; otherwise returns false.

See also insertColumns(), insertRow(), and removeColumn().

### bool QAbstractItemModel::insertColumns ( int column, int count, const QModelIndex & parent = QModelIndex() ) `[virtual]`

Inserts count new columns into the model before the given column. The items in each new column will be children of the item represented by the parent model index.

If column is 0, the columns are prepended to any existing columns. If column is columnCount(), the columns are appended to any existing columns. If parent has no children, a single row with count columns is inserted.

Returns true if the columns were successfully inserted; otherwise returns false.

The base class implementation does nothing and returns false.

If you want to be able to insert columns in a subclass, you must reimplement this function, calling the beginInsertColumns() function before inserting new columns into your underlying data store, and call the endInsertColumns() function afterwards. Return true to indicate success; otherwise return false.

### bool QAbstractItemModel::insertRow ( int row, const QModelIndex & parent = QModelIndex() )

Inserts a single row before the given row in the child items of the parent specified. Returns true if the row is inserted; otherwise returns false.

See also insertRows(), insertColumn(), and removeRow().

### bool QAbstractItemModel::insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() ) `[virtual]`

Inserts count rows into the model before the given row. The items in the new row will be children of the item represented by the parent model index.

If row is 0, the rows are prepended to any existing rows in the parent. If row is rowCount(), the rows are appended to any existing rows in the parent. If parent has no children, a single column with count rows is inserted.

Returns true if the rows were successfully inserted; otherwise returns false.

The base class implementation does nothing and returns false.

If you want to be able to insert rows in a subclass, you must reimplement this function, calling the beginInsertRows() function before inserting new rows into your underlying data store, and call the endInsertRows() function afterwards. Return true to indicate success; otherwise return false.

### QMap<int, QVariant> QAbstractItemModel::itemData ( const QModelIndex & index ) const `[virtual]`

Returns a map with values for all predefined roles in the model for the item at the given index.

This must be reimplemented if you want to extend the model with custom roles.

See also setItemData(), Qt::ItemDataRole, and data().

### void QAbstractItemModel::layoutChanged () `[signal]`

This signal is emitted whenever the layout of items exposed by the model changes; for example, when the model is sorted. When this signal is received by a view, it should update the layout of items to reflect this change.

When subclassing QAbstractItemModel or QProxyModel, ensure that you emit this signal if you change the order of items or alter the structure of the data you expose to views.

See also dataChanged(), headerDataChanged(), and reset().

### QModelIndexList QAbstractItemModel::match ( const QModelIndex & start, int role, const QVariant & value, int hits = 1, Qt::MatchFlags flags = Qt::MatchFlags( Qt::MatchStartsWith | Qt::MatchWrap ) ) const `[virtual]`

Returns a list of indexes for the items where the data stored under the given role matches the specified value. The way the search is performed is defined by the flags given. The list that is returned may be empty.

The search starts from the start index, and continues until the number of matching data items equals hits, the search reaches the last row, or the search reaches start again, depending on whether `MatchWrap` is specified in flags.

### QMimeData * QAbstractItemModel::mimeData ( const QModelIndexList & indexes ) const `[virtual]`

Returns an object that contains a serialized description of the specified indexes. The format used to describe the items corresponding to the indexes is obtained from the mimeTypes() function.

If the list of indexes is empty, 0 is returned rather than a serialized empty list.

### QStringList QAbstractItemModel::mimeTypes () const `[virtual]`

Returns a list of MIME types that can be used to describe a list of model indexes.

See also mimeData().

### QModelIndex QAbstractItemModel::parent ( const QModelIndex & index ) const `[pure virtual]`

Returns the parent of the model item with the given index.

### bool QAbstractItemModel::removeColumn ( int column, const QModelIndex & parent = QModelIndex() )

Removes the given column from the child items of the parent specified. Returns true if the column is removed; otherwise returns false.

See also removeColumns(), removeRow(), and insertColumn().

### bool QAbstractItemModel::removeColumns ( int column, int count, const QModelIndex & parent = QModelIndex() ) `[virtual]`

Removes count columns starting with the given column under parent parent from the model. Returns true if the columns were successfully removed; otherwise returns false.

The base class implementation does nothing and returns false.

If you want to be able to remove columns in a subclass, you must reimplement this function, calling the beginRemoveColumns() function before removing new columns into your underlying data store, and call the endRemoveColumns() function afterwards. Return true to indicate success; otherwise return false.

See also removeColumn(), removeRows(), and insertColumns().

### bool QAbstractItemModel::removeRow ( int row, const QModelIndex & parent = QModelIndex() )

Removes the given row from the child items of the parent specified. Returns true if the row is removed; otherwise returns false.

See also removeRows(), removeColumn(), and insertRow().

### bool QAbstractItemModel::removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() ) `[virtual]`

Removes count rows starting with the given row under parent parent from the model. Returns true if the rows were successfully removed; otherwise returns false.

The base class implementation does nothing and returns false.

If you want to be able to remove rows in a subclass, you must reimplement this function, calling the beginRemoveRows() function before removing new rows into your underlying data store, and call the endRemoveRows() function afterwards. Return true to indicate success; otherwise return false.

See also removeRow(), removeColumns(), and insertColumns().

### void QAbstractItemModel::reset () `[protected]`

Resets the model to its original state in any attached views.

When a model is reset it means that any previous data reported from the model is now invalid and has to be queried for again.

When a model radically changes its data it can sometimes be easier to just emit this signal rather than dataChanged().

### void QAbstractItemModel::revert () `[virtual slot]`

Called to let the model know that it should discart whatever it has cached. Typically used for row editing.

### int QAbstractItemModel::rowCount ( const QModelIndex & parent = QModelIndex() ) const `[pure virtual]`

Returns the number of rows under the given parent.

### bool QAbstractItemModel::setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole ) `[virtual]`

Sets the role data for the item at index to value. Returns true if successful; otherwise returns false.

The base class implementation returns false. This function and data() must be reimplemented for editable models.

See also Qt::ItemDataRole, data(), and itemData().

### bool QAbstractItemModel::setHeaderData ( int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole ) `[virtual]`

Sets the role for the header section to value. The orientation gives the orientation of the header.

See also headerData().

### bool QAbstractItemModel::setItemData ( const QModelIndex & index, const QMap<int, QVariant> & roles ) `[virtual]`

For every Qt::ItemDataRole in roles, sets the role data for the item at index to the associated value in roles. Returns true if successful; otherwise returns false.

See also setData(), data(), and itemData().

### QModelIndex QAbstractItemModel::sibling ( int row, int column, const QModelIndex & index ) const

Returns the sibling at row and column for the item at index or an invalid QModelIndex if there is no sibling.

row, column, and index.

### void QAbstractItemModel::sort ( int column, Qt::SortOrder order = Qt::AscendingOrder ) `[virtual]`

Sorts the model by column in the given order.

The base class implementation does nothing.

### QSize QAbstractItemModel::span ( const QModelIndex & index ) const `[virtual]`

Returns the row and column span of the item represented by index.

### bool QAbstractItemModel::submit () `[virtual slot]`

Called to let the model know that it should submit whatever it has cached to the permanent storage. Typically used for row editing.

Returns false on error, otherwise true.

### Qt::DropActions QAbstractItemModel::supportedDropActions () const `[virtual]`

Returns the drop actions supported by this model.

The default implementation returns Qt::CopyAction. It is only necessary to reimplement this function in subclasses if you wish to support more types of drag and drop operation.

See also Qt::DropActions.