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, QAbstractProxyModel, QAbstractTableModel, QDirModel, QProxyModel, and QStandardItemModel.

Public Functions

Public Slots

Signals

Protected Functions

Additional Inherited Members


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(), 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.

It is not necessary to support every role defined in Qt::ItemDataRole. Depending on the type of data contained within a model, it may only be useful to implement the data() function to return valid information for some of the more common roles. Most models provide at least a textual representation of item data for the Qt::DisplayRole, and well-behaved models should also provide valid information for the Qt::ToolTipRole and Qt::WhatsThisRole. Supporting these roles enables models to be used with standard Qt views. However, for some models that handle highly-specialized data, it may be appropriate to provide data only for user-defined roles.

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:

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, QAbstractItemView, and Using Drag and Drop with Item Views.


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.

void QAbstractItemModel::changePersistentIndexList ( const QModelIndexList & from, const QModelIndexList & to )   [protected]

Changes the QPersistentModelIndexes that is equal to the indexes in the given from model index list to the given to model index list.

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

This function was introduced in Qt 4.1.

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

Returns the number of columns for the given parent.

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

This signal is emitted just before columns are inserted into the model. The new items will be positioned between start and end inclusive, under the given parent item.

See also insertColumns().

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

This signal is emitted just before columns are removed from the model. The items to be removed are those between start and end inclusive, under the given parent item.

See also removeColumns().

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().

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

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

See also removeColumns().

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. Use rowCount() on the parent to find out the number of children.

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]

On models that support this, 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 implement your own model, you can reimplement this function if you want to support insertions. Alternatively, you can provide you own API for altering the data.

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]

On models that support this, 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 implement your own model, you can reimplement this function if you want to support insertions. Alternatively, you can provide you own API for altering the data.

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 serialized items of data corresponding to the list of indexes specified. The formats used to describe the encoded data 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().

void QAbstractItemModel::modelReset ()   [signal]

This signal is emitted when reset() is called.

This function was introduced in Qt 4.1.

See also reset().

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]

On models that support this, 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 implement your own model, you can reimplement this function if you want to support removing. Alternatively, you can provide you own API for altering the data.

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]

On models that support this, 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 implement your own model, you can reimplement this function if you want to support removing. Alternatively, you can provide you own API for altering the data.

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 call this function rather than emit dataChanged() to inform other components when the underlying data source, or its structure, has changed.

See also modelReset().

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::rowsAboutToBeInserted ( const QModelIndex & parent, int start, int end )   [signal]

This signal is emitted just before rows are inserted into the model. The new items will be positioned between start and end inclusive, under the given parent item.

See also insertRows().

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

This signal is emitted just before rows are removed from the model. The items that will be removed are those between start and end inclusive, under the given parent item.

See also removeRows().

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().

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

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

See also removeRows().

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.


Copyright © 2005 Trolltech Trademarks
Qt 4.1.0
Hosted by uCoz