Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QAbstractItemModel Class Reference

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

    #include <QAbstractItemModel>

Inherits QObject.

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

- List of all members, including inherited members

### Public Types

- enum ItemFlag { ItemIsSelectable, ItemIsEditable, ItemIsDragEnabled, ItemIsDropEnabled, ItemIsCheckable, ItemIsEnabled }
- typedef ItemFlags
- enum MatchFlag { MatchContains, MatchFromStart, MatchFromEnd, MatchExactly, MatchCase, ..., MatchDefault }
- typedef MatchFlags
- enum Role { DisplayRole, DecorationRole, EditRole, ToolTipRole, StatusTipRole, ..., UserRole }

### Writable Properties

- 1 writable property inherited from QObject

### Public Functions

- QAbstractItemModel ( QObject * parent = 0 )
- virtual ~QAbstractItemModel ()
- virtual QModelIndex buddy ( const QModelIndex & index ) const
- virtual int columnCount ( const QModelIndex & parent = QModelIndex() ) const = 0
- virtual QVariant data ( const QModelIndex & index, int role = DisplayRole ) const = 0
- virtual bool dropMimeData ( const QMimeData * data, QDrag::DropAction action, int row, const QModelIndex & parent )
- virtual void fetchMore ( const QModelIndex & parent )
- virtual ItemFlags flags ( const QModelIndex & index ) const
- virtual bool hasChildren ( const QModelIndex & parent = QModelIndex() ) const = 0
- bool hasIndex ( int row, int column, const QModelIndex & parent = QModelIndex() ) const
- virtual QVariant headerData ( int section, Qt::Orientation o, int role = 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, MatchFlags flags = MatchDefault ) 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 = EditRole )
- virtual bool setHeaderData ( int section, Qt::Orientation orientation, const QVariant & value, int role = 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 QDrag::DropActions supportedDropActions () const

- 26 public functions inherited from QObject

### Public Slots

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

- 1 public slot inherited from QObject

### Signals

- void columnsAboutToBeRemoved ( const QModelIndex & parent, int first, int last )
- void columnsInserted ( const QModelIndex & parent, int start, int end )
- void dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight )
- void headerDataChanged ( Qt::Orientation orientation, int first, int last )
- void rowsAboutToBeRemoved ( const QModelIndex & parent, int first, int last )
- void rowsInserted ( const QModelIndex & parent, int start, int end )

- 1 signal inherited from QObject

### Static Public Members

- 4 static public members inherited from QObject

### Protected Functions

- QModelIndex createIndex ( int row, int column, void * data = 0 ) const

- 7 protected functions 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.

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 has an index(), and possibly a sibling() index; child items have a parent() index. hasChildren() is true for items that have children. Each item has a number of data elements associated with them, each with a particular `Role`. Data elements are set individually with setData(), or for all roles with setItemData(). Data is retrieved from an item with data() (for a single role), or with itemData() (for every role). Items can be queried with flags() (see {QAbstractTableModel::ItemFlag}) to see if they are selectable, dragable etc. An item can be searched for using match().

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 the contents of the model are changed; rowsInserted(), columnsInserted(), rowsRemoved(), and columnsRemoved() are emitted when the model's dimensions are changed.

If the model isSortable(), it can be sorted with sort(). To customize sorting and searching, comparison functions can be reimplemented; for example, lessThan(), equal(), and greaterThan().

When subclassing QAbstractItemModel, at the very least you must implement index(), parent(), rowCount(), columnCount(), and data(). To enable editing in your model, you must also implement flags() to return ItemIsEditable and setData().

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

## Member Type Documentation

### enum QAbstractItemModel::ItemFlag
typedef QAbstractItemModel::ItemFlags

This enum describes the properties of an item:

`QAbstractItemModel::ItemIsSelectable` | It can be selected.
`QAbstractItemModel::ItemIsEditable` | It can be edited.
`QAbstractItemModel::ItemIsDragEnabled` | It can be dragged.
`QAbstractItemModel::ItemIsDropEnabled` | It can be used as a drop target.
`QAbstractItemModel::ItemIsCheckable` | It can be checked.
`QAbstractItemModel::ItemIsEnabled` | The user can interact with the item.

The ItemFlags typedef can store a combination of ItemFlag values.

### enum QAbstractItemModel::MatchFlag
typedef QAbstractItemModel::MatchFlags

`QAbstractItemModel::MatchContains` | The value is contained in the item.
`QAbstractItemModel::MatchFromStart` | The value matches the start of the item.
`QAbstractItemModel::MatchFromEnd` | The value matches the end of the item.
`QAbstractItemModel::MatchExactly` | The value matches the item exactly.
`QAbstractItemModel::MatchCase` | The search is case sensitive.
`QAbstractItemModel::MatchWrap` | The search wraps around.
`QAbstractItemModel::MatchDefault` | The default match, which is MatchFromStart|MatchWrap.

The MatchFlags typedef can store a combination of MatchFlag values.

### enum QAbstractItemModel::Role

Each item in the model has a set of data elements associated with it, each with its own role. The roles are used by the view to indicate to the model which type of data it needs.

The general purpose roles are:

`QAbstractItemModel::DisplayRole` | The key data to be rendered (usually text).
`QAbstractItemModel::DecorationRole` | The data to be rendered as a decoration (usually an icon).
`QAbstractItemModel::EditRole` | The data in a form suitable for editing in an editor.
`QAbstractItemModel::ToolTipRole` | The data displayed in the item's tooltip.
`QAbstractItemModel::StatusTipRole` | The data displayed in the status bar.
`QAbstractItemModel::WhatsThisRole` | The data displayed for the item in "What's This?" mode.

Roles describing appearance and meta data:

`QAbstractItemModel::FontRole` | The font used for items rendered with the default delegate.
`QAbstractItemModel::TextAlignmentRole` | The alignment of the text for items rendered with the default delegate.
`QAbstractItemModel::BackgroundColorRole` | The background color used for items rendered with the default delegate.
`QAbstractItemModel::TextColorRole` | The text color used for items rendered with the default delegate.
`QAbstractItemModel::CheckStateRole` | Whether the item can be checked.

Accessibility roles:

`QAbstractItemModel::AccessibleTextRole` | The text to be used by accessibility extensions and plugins, such as screen readers.
`QAbstractItemModel::AccessibleDescriptionRole` | A description of the item for accessibility purposes.
`QAbstractItemModel::UserRole` | The first role that can be used for application-specific purposes.

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

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

Returns the buddy of the item represented by index. When the user wants to edit an item the delegate will ask for the item's buddy, and item instead. In the default implementation an item is it's own buddy.

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

Returns the number of columns for the given parent.

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

### void QAbstractItemModel::columnsInserted ( const QModelIndex & parent, int start, int end ) `[signal]`

This signal is emitted after columns have been inserted into the model. The new items are those between start and end inclusive, under the given parent item.

See also insertColumns().

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

Creates a model index for the given row and column that points to the given data.

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

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

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

### 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 rowsInserted(), rowsRemoved(), columnsInserted(), columnsRemoved(), and setData().

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

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

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

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

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

### 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 o, int role = DisplayRole ) const `[virtual]`

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

### 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() and setHeaderData().

### 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 in 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 you must reimplement this function.

### 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 you must reimplement this function.

### 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 Role and data().

### QModelIndexList QAbstractItemModel::match ( const QModelIndex & start, int role, const QVariant & value, int hits = 1, MatchFlags flags = MatchDefault ) 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.

See also QAbstractItemModel::MatchFlag.

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

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

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

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

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

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

### void QAbstractItemModel::rowsInserted ( const QModelIndex & parent, int start, int end ) `[signal]`

This signal is emitted after rows have been inserted into the model. The new items are those between start and end inclusive, under the given parent item.

See also insertRows().

### bool QAbstractItemModel::setData ( const QModelIndex & index, const QVariant & value, int role = 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 data() and itemData().

### bool QAbstractItemModel::setHeaderData ( int section, Qt::Orientation orientation, const QVariant & value, int role = 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 `Role` 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 model index of the sibling of the item specified by the given 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.

See also isSortable().

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

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