QAbstractItemModel 類

The QAbstractItemModel 類為項模型類提供抽象接口。 更多...

頭: #include <QAbstractItemModel>
qmake: QT += core
繼承: QObject
繼承者: QAbstractListModel , QAbstractProxyModel ,和 QAbstractTableModel

公共類型

enum CheckIndexOption { NoOption, IndexIsValid, DoNotUseParent, ParentIsInvalid }
flags CheckIndexOptions
enum LayoutChangeHint { NoLayoutChangeHint, VerticalSortHint, HorizontalSortHint }

公共函數

QAbstractItemModel (QObject * parent = nullptr)
virtual ~QAbstractItemModel ()
virtual QModelIndex buddy (const QModelIndex & index ) const
virtual bool canDropMimeData (const QMimeData * data , Qt::DropAction action , int row , int column , const QModelIndex & parent ) const
virtual bool canFetchMore (const QModelIndex & parent ) const
bool checkIndex (const QModelIndex & index , QAbstractItemModel::CheckIndexOptions options = CheckIndexOption::NoOption) 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
bool moveColumn (const QModelIndex & sourceParent , int sourceColumn , const QModelIndex & destinationParent , int destinationChild )
virtual bool moveColumns (const QModelIndex & sourceParent , int sourceColumn , int count , const QModelIndex & destinationParent , int destinationChild )
bool moveRow (const QModelIndex & sourceParent , int sourceRow , const QModelIndex & destinationParent , int destinationChild )
virtual bool moveRows (const QModelIndex & sourceParent , int sourceRow , int count , const QModelIndex & destinationParent , int destinationChild )
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 QHash<int, QByteArray> roleNames () const
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 )
virtual 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 supportedDragActions () const
virtual Qt::DropActions supportedDropActions () const

公共槽

virtual void revert ()
virtual bool submit ()

信號

void columnsAboutToBeInserted (const QModelIndex & parent , int first , int last )
void columnsAboutToBeMoved (const QModelIndex & sourceParent , int sourceStart , int sourceEnd , const QModelIndex & destinationParent , int destinationColumn )
void columnsAboutToBeRemoved (const QModelIndex & parent , int first , int last )
void columnsInserted (const QModelIndex & parent , int first , int last )
void columnsMoved (const QModelIndex & parent , int start , int end , const QModelIndex & destination , int column )
void columnsRemoved (const QModelIndex & parent , int first , int last )
void dataChanged (const QModelIndex & topLeft , const QModelIndex & bottomRight , const QVector<int> & roles = ...)
void headerDataChanged (Qt::Orientation orientation , int first , int last )
void layoutAboutToBeChanged (const QList<QPersistentModelIndex> & parents = ..., QAbstractItemModel::LayoutChangeHint hint = ...)
void layoutChanged (const QList<QPersistentModelIndex> & parents = ..., QAbstractItemModel::LayoutChangeHint hint = ...)
void modelAboutToBeReset ()
void modelReset ()
void rowsAboutToBeInserted (const QModelIndex & parent , int start , int end )
void rowsAboutToBeMoved (const QModelIndex & sourceParent , int sourceStart , int sourceEnd , const QModelIndex & destinationParent , int destinationRow )
void rowsAboutToBeRemoved (const QModelIndex & parent , int first , int last )
void rowsInserted (const QModelIndex & parent , int first , int last )
void rowsMoved (const QModelIndex & parent , int start , int end , const QModelIndex & destination , int row )
void rowsRemoved (const QModelIndex & parent , int first , int last )

靜態公共成員

const QMetaObject staticMetaObject

保護函數

void beginInsertColumns (const QModelIndex & parent , int first , int last )
void beginInsertRows (const QModelIndex & parent , int first , int last )
bool beginMoveColumns (const QModelIndex & sourceParent , int sourceFirst , int sourceLast , const QModelIndex & destinationParent , int destinationChild )
bool beginMoveRows (const QModelIndex & sourceParent , int sourceFirst , int sourceLast , const QModelIndex & destinationParent , int destinationChild )
void beginRemoveColumns (const QModelIndex & parent , int first , int last )
void beginRemoveRows (const QModelIndex & parent , int first , int last )
void beginResetModel ()
void changePersistentIndex (const QModelIndex & from , const QModelIndex & to )
void changePersistentIndexList (const QModelIndexList & from , const QModelIndexList & to )
QModelIndex createIndex (int row , int column , void * ptr = nullptr) const
QModelIndex createIndex (int row , int column , quintptr id ) const
void endInsertColumns ()
void endInsertRows ()
void endMoveColumns ()
void endMoveRows ()
void endRemoveColumns ()
void endRemoveRows ()
void endResetModel ()
QModelIndexList persistentIndexList () const

保護槽

void resetInternalData ()

額外繼承成員

詳細描述

The QAbstractItemModel 類為項模型類提供抽象接口。

The QAbstractItemModel 類定義項模型必須使用的標準接口,為能與模型/視圖體係結構中的其它組件互操作。假設無法直接實例化它。相反,應子類它以創建新模型。

The QAbstractItemModel 類是一種 模型/視圖類 且屬於 Qt 的 模型/視圖框架 。可以將它用作 QML 項視圖元素 (或 Qt Widgets 模塊項視圖類) 的底層數據模型。

若需要用於項視圖的模型,譬如 QML 列錶視圖元素或 C++ 小部件 QListView or QTableView ,應考慮子類化 QAbstractListModel or QAbstractTableModel 而不是此類。

底層數據模型以錶格層次結構形式被暴露給視圖和委托。若不使用層次結構,那麼模型是簡單行列錶。每項擁有唯一索引指定通過 QModelIndex .

可以憑藉模型訪問的各數據項,都擁有關聯模型索引。可以獲取此模型索引使用 index () 函數。每個索引可以擁有 sibling () 索引;子級項擁有 parent () 索引。

各項有許多與之關聯的數據元素,且可以檢索它們通過指定角色 (見 Qt::ItemDataRole ) 到模型的 data () 函數。可以同時獲得所有可用角色的數據使用 itemData () 函數。

各角色數據的設置是使用特定 Qt::ItemDataRole 。單個角色數據的設置是單獨采用 setData (),或者可以設置所有角色采用 setItemData ().

可以查詢項采用 flags () (見 Qt::ItemFlag ) 以查看是否可以選中、拖拽或以其它方式操縱它們。

若項擁有子級對象, hasChildren () 返迴 true 對於相應索引。

模型有 rowCount () 和 columnCount () 對於層次結構各級彆。插入/移除行和列可以采用 insertRows (), insertColumns (), removeRows (),和 removeColumns ().

模型通過發射信號,來指示改變。例如, dataChanged () 被發射,每當模型可用數據項改變時。改變由模型提供的 Header (頭) 會導緻 headerDataChanged () 被發射。若底層數據的結構改變,模型會發射 layoutChanged () 以指示任何附加視圖,它們應考慮新結構,重新顯示任何展示項。

透過模型中的可用項搜索特定數據,可以使用 match () 函數。

要排序模型,可以使用 sort ().

子類化

注意: 用於子類化模型的一些一般可用指導方針,在 模型子類化參考 .

當子類化 QAbstractItemModel , at the very least you must implement index (), parent (), rowCount (), columnCount (),和 data ()。在所有隻讀模型使用這些函數,並形成可編輯模型的基礎。

還可以重實現 hasChildren () 為模型提供特殊行為,其中實現的 rowCount () 很昂貴。這使模型限定由視圖請求的數據量成為可能,且可以用作實現模型數據惰性填充的方式。

要啓用模型編輯,還必須實現 setData (),並重實現 flags () 以確保 ItemIsEditable 被返迴。還可以重實現 headerData () 和 setHeaderData () 以控製模型呈現標題的方式。

The dataChanged () 和 headerDataChanged () 信號必須被明確發射,當重實現 setData () 和 setHeaderData () 函數,分彆。

自定義模型需要創建模型索引,以供其它組件使用。要做到這,調用 createIndex () 采用針對項的閤適行號和列號,及其標識符 (要麼以指針方式,要麼以整數值方式)。這些值的組閤必須唯一,對於各項。通常,自定義模型會在其它重實現函數中使用這些唯一標識符,以檢索項數據並訪問項父級和子級的有關信息。見 簡單樹模型範例 瞭解唯一標識符的更多有關信息。

沒有必要支持的各角色,其定義在 Qt::ItemDataRole 。從屬模型包含的數據類型,它可能纔有用為實現 data () 函數,以返迴用於一些更常見角色的有效信息。大多數模型至少提供項數據的正文錶示為 Qt::DisplayRole ,且行為良好模型還應提供有效信息為 Qt::ToolTipRole and Qt::WhatsThisRole 。支持這些角色,使模型能夠用於標準 Qt 視圖。不管怎樣,對於處理高度專用數據的某些模型,僅為用戶定義角色提供數據可能是閤適的。

為可重置大小的數據結構提供接口的模型,可以提供實現 insertRows (), removeRows (), insertColumns (),and removeColumns ()。當實現這些函數時,重要的是通知任何已連接視圖,即將改變模型維度 before and after 它們齣現:

The private 信號這些函數發射給定附加組件改變,以在任何數據變為不可用前采取行動。采用這些 begin 和 end 函數封裝地插入和移除操作,還使模型能夠管理 持久模型索引 正確。 若想要正確處理選擇,必須確保調用這些函數。 若插入 (或移除) 具有子級的項,不需要為子級項調用這些函數。換句話說,父級項會關心其子級項。

要創建增量填充模型,可以重實現 fetchMore () 和 canFetchMore ()。若重實現 fetchMore () 把行添加到模型, beginInsertRows() and endInsertRows() 必須被調用。

另請參閱 模型類 , 模型子類化參考 , QModelIndex , QAbstractItemView , 將拖放用於項視圖 , 簡單 DOM 模型範例 , 簡單樹模型範例 , 可編輯樹模型範例 ,和 抓取更多範例 .

成員類型文檔編製

enum QAbstractItemModel:: CheckIndexOption
flags QAbstractItemModel:: CheckIndexOptions

此枚舉可以用於控製校驗,履行通過 QAbstractItemModel::checkIndex ().

常量 描述
QAbstractItemModel::NoOption 0x0000 未指定校驗選項。
QAbstractItemModel::IndexIsValid 0x0001 模型索引被傳遞給 QAbstractItemModel::checkIndex () 被校驗成有效模型索引。
QAbstractItemModel::DoNotUseParent 0x0002 不履行任何涉及索引父級用法的校驗,被傳遞給 QAbstractItemModel::checkIndex ().
QAbstractItemModel::ParentIsInvalid 0x0004 模型索引的父級被傳遞給 QAbstractItemModel::checkIndex () 被校驗成無效模型索引。若此選項和 DoNotUseParent 兩者有指定,那麼忽略此選項。

該枚舉在 Qt 5.11 引入 (或被修改)。

CheckIndexOptions 類型是 typedef 對於 QFlags <CheckIndexOption>。它存儲 CheckIndexOption 值的 OR (或) 組閤。

enum QAbstractItemModel:: LayoutChangeHint

此枚舉描述模型改變布局的方式。

常量 描述
QAbstractItemModel::NoLayoutChangeHint 0 沒有提示可用。
QAbstractItemModel::VerticalSortHint 1 正排序行。
QAbstractItemModel::HorizontalSortHint 2 正排序列。

注意,VerticalSortHint 和 HorizontalSortHint 攜帶的意思是項在同一父級中移動,未移動到模型中的不同父級,且沒有濾齣 (或濾入)。

成員函數文檔編製

QAbstractItemModel:: QAbstractItemModel ( QObject * parent = nullptr)

構造抽象項模型采用給定 parent .

[虛擬] QAbstractItemModel:: ~QAbstractItemModel ()

銷毀抽象項模型。

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

開始列插入操作。

當重實現 insertColumns () 在子類,必須調用此函數 before 把數據插入模型的底層數據存儲。

The parent 索引相當於被插入新列的父級; first and last 是插入後新列將擁有的列號。

Inserting columns

為想要插入模型中項的列跨度,指定第一列和最後列號。

例如,如簡圖所示,在第 4 列前插入 3 列,因此 first 為 4 和 last 為 6:

beginInsertColumns(parent, 4, 6);
								

這把 3 新列插入作為列 4、5 和 6。

Appending columns

要追加列,把它們插入在最後一列之後。

例如,如簡圖所示,把 3 列追加到 6 現有列的集閤 (以列 5 結尾),因此 first 為 6 和 last 為 8:

beginInsertColumns(parent, 6, 8);
								

這把 3 新列追加作為列 6、7 和 8。

注意: 此函數發射 columnsAboutToBeInserted () 信號,必須處理連接的視圖 (或代理) 在插入數據之前。否則,視圖可能以無效狀態結束。

另請參閱 endInsertColumns ().

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

開始行插入操作。

當重實現 insertRows () 在子類,必須調用此函數 before 把數據插入模型的底層數據存儲。

The parent 索引相當於被插入新行的父級; first and last 是插入後新行擁有的行號。

Inserting rows

為想要插入模型中項的行跨度,指定第一列和最後行號。

例如,如簡圖所示,在第 2 行前插入 3 行,因此 first 為 2 和 last 為 4:

beginInsertRows(parent, 2, 4);
								

這把 3 新行插入作為行 2、3 和 4。

Appending rows

要追加行,把它們插入在最後一行之後。

例如,如簡圖所示,把 2 行追加到 4 現有行的集閤 (以行 3 結尾),因此 first 為 4 和 last 是 5:

beginInsertRows(parent, 4, 5);
								

這把 2 新行追加作為行 4 和 5。

注意: 此函數發射 rowsAboutToBeInserted () 信號,必須處理連接的視圖 (或代理) 在插入數據之前。否則,視圖可能以無效狀態結束。

另請參閱 endInsertRows ().

[protected] bool QAbstractItemModel:: beginMoveColumns (const QModelIndex & sourceParent , int sourceFirst , int sourceLast , const QModelIndex & destinationParent , int destinationChild )

開始列移動操作。

When reimplementing a subclass, this method simplifies moving entities in your model. This method is responsible for moving persistent indexes in the model, which you would otherwise be required to do yourself. Using beginMoveColumns and endMoveColumns is an alternative to emitting layoutAboutToBeChanged and layoutChanged directly along with changePersistentIndex .

The sourceParent index corresponds to the parent from which the columns are moved; sourceFirst and sourceLast are the first and last column numbers of the columns to be moved. The destinationParent index corresponds to the parent into which those columns are moved. The destinationChild is the column to which the columns will be moved. That is, the index at column sourceFirst in sourceParent will become column destinationChild in destinationParent , followed by all other columns up to sourceLast .

However, when moving columns down in the same parent ( sourceParent and destinationParent are equal), the columns will be placed before the destinationChild index. That is, if you wish to move columns 0 and 1 so they will become columns 1 and 2, destinationChild should be 3. In this case, the new index for the source column i (which is between sourceFirst and sourceLast ) is equal to (destinationChild-sourceLast-1+i) .

注意,若 sourceParent and destinationParent are the same, you must ensure that the destinationChild is not within the range of sourceFirst and sourceLast + 1. You must also ensure that you do not attempt to move a column to one of its own children or ancestors. This method returns false if either condition is true, in which case you should abort your move operation.

該函數在 Qt 4.6 引入。

另請參閱 endMoveColumns ().

[protected] bool QAbstractItemModel:: beginMoveRows (const QModelIndex & sourceParent , int sourceFirst , int sourceLast , const QModelIndex & destinationParent , int destinationChild )

開始行移動操作。

When reimplementing a subclass, this method simplifies moving entities in your model. This method is responsible for moving persistent indexes in the model, which you would otherwise be required to do yourself. Using beginMoveRows and endMoveRows is an alternative to emitting layoutAboutToBeChanged and layoutChanged directly along with changePersistentIndex .

The sourceParent index corresponds to the parent from which the rows are moved; sourceFirst and sourceLast are the first and last row numbers of the rows to be moved. The destinationParent index corresponds to the parent into which those rows are moved. The destinationChild is the row to which the rows will be moved. That is, the index at row sourceFirst in sourceParent will become row destinationChild in destinationParent , followed by all other rows up to sourceLast .

However, when moving rows down in the same parent ( sourceParent and destinationParent are equal), the rows will be placed before the destinationChild index. That is, if you wish to move rows 0 and 1 so they will become rows 1 and 2, destinationChild should be 3. In this case, the new index for the source row i (which is between sourceFirst and sourceLast ) is equal to (destinationChild-sourceLast-1+i) .

注意,若 sourceParent and destinationParent are the same, you must ensure that the destinationChild is not within the range of sourceFirst and sourceLast + 1. You must also ensure that you do not attempt to move a row to one of its own children or ancestors. This method returns false if either condition is true, in which case you should abort your move operation.

Moving rows to another parent

Specify the first and last row numbers for the span of rows in the source parent you want to move in the model. Also specify the row in the destination parent to move the span to.

For example, as shown in the diagram, we move three rows from row 2 to 4 in the source, so sourceFirst 為 2 和 sourceLast is 4. We move those items to above row 2 in the destination, so destinationChild is 2.

beginMoveRows(sourceParent, 2, 4, destinationParent, 2);
								

This moves the three rows rows 2, 3, and 4 in the source to become 2, 3 and 4 in the destination. Other affected siblings are displaced accordingly.

Moving rows to append to another parent

To append rows to another parent, move them to after the last row.

For example, as shown in the diagram, we move three rows to a collection of 6 existing rows (ending in row 5), so destinationChild 為 6:

beginMoveRows(sourceParent, 2, 4, destinationParent, 6);
								

This moves the target rows to the end of the target parent as 6, 7 and 8.

Moving rows in the same parent up

To move rows within the same parent, specify the row to move them to.

For example, as shown in the diagram, we move one item from row 2 to row 0, so sourceFirst and sourceLast are 2 and destinationChild 為 0。

beginMoveRows(parent, 2, 2, parent, 0);
								

Note that other rows may be displaced accordingly. Note also that when moving items within the same parent you should not attempt invalid or no-op moves. In the above example, item 2 is at row 2 before the move, so it can not be moved to row 2 (where it is already) or row 3 (no-op as row 3 means above row 3, where it is already)

Moving rows in the same parent down

To move rows within the same parent, specify the row to move them to.

For example, as shown in the diagram, we move one item from row 2 to row 4, so sourceFirst and sourceLast are 2 and destinationChild is 4.

beginMoveRows(parent, 2, 2, parent, 4);
								

Note that other rows may be displaced accordingly.

該函數在 Qt 4.6 引入。

另請參閱 endMoveRows ().

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

開始列移除操作。

當重實現 removeColumns () 在子類,必須調用此函數 before 移除數據從模型的底層數據存儲。

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

Removing columns

Specify the first and last column numbers for the span of columns you want to remove from an item in a model.

For example, as shown in the diagram, we remove the three columns from column 4 to column 6, so first 為 4 和 last 為 6:

beginRemoveColumns(parent, 4, 6);
								

注意: 此函數發射 columnsAboutToBeRemoved () signal which connected views (or proxies) must handle before the data is removed. Otherwise, the views may end up in an invalid state.

另請參閱 endRemoveColumns ().

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

開始行移除操作。

當重實現 removeRows () 在子類,必須調用此函數 before 移除數據從模型的底層數據存儲。

The parent 索引相當於從其中移除新行的父級; first and last 是要移除行的行號。

Removing rows

Specify the first and last row numbers for the span of rows you want to remove from an item in a model.

For example, as shown in the diagram, we remove the two rows from row 2 to row 3, so first 為 2 和 last is 3:

beginRemoveRows(parent, 2, 3);
								

注意: 此函數發射 rowsAboutToBeRemoved () signal which connected views (or proxies) must handle before the data is removed. Otherwise, the views may end up in an invalid state.

另請參閱 endRemoveRows ().

[protected] void QAbstractItemModel:: beginResetModel ()

開始模型重置操作。

A reset operation resets the model to its current 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. This also means that the current item and any selected items will become invalid.

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.

You must call this function before resetting any internal data structures in your model or proxy model.

此函數發射信號 modelAboutToBeReset ().

該函數在 Qt 4.6 引入。

另請參閱 modelAboutToBeReset (), modelReset (),和 endResetModel ().

[虛擬] QModelIndex QAbstractItemModel:: buddy (const QModelIndex & index ) const

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. Then, the view will construct a delegate using the model index returned by the buddy item.

The default implementation of this function has each item as its own buddy.

[虛擬] bool QAbstractItemModel:: canDropMimeData (const QMimeData * data , Qt::DropAction action , int row , int column , const QModelIndex & parent ) const

返迴 true 若模型可以接受掉落的 data 。此默認實現纔校驗若 data 至少有 1 種格式在列錶化的 mimeTypes () 且若 action 是在模型的 supportedDropActions ().

在自定義模型中重新實現此函數,若想要測試是否 data 可以掉落在 row , column , parent with action 。若不需要該測試,就不必重新實現此函數。

另請參閱 dropMimeData () 和 將拖放用於項視圖 .

[虛擬] bool QAbstractItemModel:: canFetchMore (const QModelIndex & parent ) const

返迴 true 若有更多數據可用對於 parent ;否則返迴 false .

默認實現始終返迴 false .

若 canFetchMore() 返迴 true fetchMore () 函數應該被調用。這種行為歸 QAbstractItemView ,例如。

注意: 此函數可以被援引,通過元對象係統和從 QML。見 Q_INVOKABLE .

另請參閱 fetchMore ().

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

改變 QPersistentModelIndex 也就是說,等於給定 from 模型索引到給定 to 模型索引。

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

另請參閱 persistentIndexList () 和 changePersistentIndexList ().

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

改變 { QPersistentModelIndex }es that are equal to the indexes in the given from model index list to the given to 模型索引列錶。

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

該函數在 Qt 4.1 引入。

另請參閱 persistentIndexList () 和 changePersistentIndex ().

bool QAbstractItemModel:: checkIndex (const QModelIndex & index , QAbstractItemModel::CheckIndexOptions options = CheckIndexOption::NoOption) const

此函數校驗是否 index is a legal model index for this model. A legal model index is either an invalid model index, or a valid model index for which all the following holds:

  • 索引的模型是 this ;
  • the index' row is greater or equal than zero;
  • the index' row is less than the row count for the index' parent;
  • the index' column is greater or equal than zero;
  • the index' column is less than the column count for the index' parent.

The options argument may change some of these checks. If options 包含 IndexIsValid ,那麼 index must be a valid index; this is useful when reimplementing functions such as data () 或 setData (), which expect valid indexes.

options 包含 DoNotUseParent , then the checks that would call parent () are omitted; this allows calling this function from a parent () reimplementation (otherwise, this would result in endless recursion and a crash).

options 未包含 DoNotUseParent ,且它包含 ParentIsInvalid , then an additional check is performed: the parent index is checked for not being valid. This is useful when implementing flat models such as lists or tables, where no model index should have a valid parent index.

This function returns true if all the checks succeeded, and false otherwise. This allows to use the function in Q_ASSERT and similar other debugging mechanisms. If some check failed, a warning message will be printed in the qt.core.qabstractitemmodel.checkindex logging category, containing some information that may be useful for debugging the failure.

注意: This function is a debugging helper for implementing your own item models. When developing complex models, as well as when building complicated model hierarchies (e.g. using proxy models), it is useful to call this function in order to catch bugs relative to illegal model indices (as defined above) accidentally passed to some QAbstractItemModel API.

警告: Note that it's undefined behavior to pass illegal indices to item models, so applications must refrain from doing so, and not rely on any "defensive" programming that item models could employ to handle illegal indexes gracefully.

該函數在 Qt 5.11 引入。

另請參閱 QModelIndex .

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

Returns the number of columns for the children of the given parent .

對於大多數子類,列數獨立於 parent .

例如:

int DomModel::columnCount(const QModelIndex &/*parent*/) const
{
    return 3;
}
					

注意: 當實現基於錶格的模型時,columnCount() 應返迴 0,當父級有效時。

注意: 此函數可以被援引,通過元對象係統和從 QML。見 Q_INVOKABLE .

另請參閱 rowCount ().

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

This signal is emitted just before columns are inserted into the model. The new items will be positioned between first and last inclusive, under the given parent 項。

注意: Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 insertColumns () 和 beginInsertColumns ().

[signal] void QAbstractItemModel:: columnsAboutToBeMoved (const QModelIndex & sourceParent , int sourceStart , int sourceEnd , const QModelIndex & destinationParent , int destinationColumn )

This signal is emitted just before columns are moved within the model. The items that will be moved are those between sourceStart and sourceEnd inclusive, under the given sourceParent item. They will be moved to destinationParent starting at the column destinationColumn .

注意: Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.

該函數在 Qt 4.6 引入。

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 beginMoveRows ().

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

This signal is emitted just before columns are removed from the model. The items to be removed are those between first and last inclusive, under the given parent 項。

注意: Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 removeColumns () 和 beginRemoveColumns ().

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

This signal is emitted after columns have been inserted into the model. The new items are those between first and last inclusive, under the given parent 項。

注意: Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 insertColumns () 和 beginInsertColumns ().

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

This signal is emitted after columns have been moved within the model. The items between start and end inclusive, under the given parent item have been moved to destination starting at the column column .

注意: Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.

該函數在 Qt 4.6 引入。

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 beginMoveRows ().

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

This signal is emitted after columns have been removed from the model. The removed items are those between first and last inclusive, under the given parent 項。

注意: Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 removeColumns () 和 beginRemoveColumns ().

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

創建模型索引為給定 row and column 采用內部指針 ptr .

當使用 QSortFilterProxyModel ,索引擁有自己的內部指針。在模型外訪問此內部指針,不可能。使用 data () 函數代替。

此函數提供必須使用模型子類,創建模型索引的一緻接口。

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

創建模型索引為給定 row and column 采用內部標識符 id .

此函數提供必須使用模型子類,創建模型索引的一緻接口。

另請參閱 QModelIndex::internalId ().

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

返迴的數據存儲在給定 role 為項引用通過 index .

注意: 若沒有值要返迴,返迴 無效 QVariant 而不是返迴 0。

注意: 此函數可以被援引,通過元對象係統和從 QML。見 Q_INVOKABLE .

另請參閱 Qt::ItemDataRole , setData (),和 headerData ().

[signal] void QAbstractItemModel:: dataChanged (const QModelIndex & topLeft , const QModelIndex & bottomRight , const QVector < int > & roles = ...)

此信號發射,每當現有項數據改變時。

If the items are of the same parent, the affected ones are those between topLeft and bottomRight inclusive. If the items do not have the same parent, the behavior is undefined.

當重實現 setData () 函數,必須明確發射此信號。

可選 roles argument can be used to specify which data roles have actually been modified. An empty vector in the roles argument means that all roles should be considered modified. The order of elements in the roles argument does not have any relevance.

另請參閱 headerDataChanged (), setData (),和 layoutChanged ().

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

處理 data 的提供是通過結束拖放操作,按給定 action .

返迴 true 若數據和動作是由模型處理的;否則返迴 false .

指定 row , column and parent indicate the location of an item in the model where the operation ended. It is the responsibility of the model to complete the action at the correct location.

For instance, a drop action on an item in a QTreeView can result in new items either being inserted as children of the item specified by row , column ,和 parent , or as siblings of the item.

row and column are -1 it means that the dropped data should be considered as dropped directly on parent . Usually this will mean appending the data as child items of parent 。若 row and column are greater than or equal zero, it means that the drop occurred just before the specified row and column 以指定 parent .

The mimeTypes () member is called to get the list of acceptable MIME types. This default implementation assumes the default implementation of mimeTypes (), which returns a single default MIME type. If you reimplement mimeTypes () in your custom model to return multiple MIME types, you must reimplement this function to make use of them.

另請參閱 supportedDropActions (), canDropMimeData (),和 將拖放用於項視圖 .

[protected] void QAbstractItemModel:: endInsertColumns ()

結束列插入操作。

當重實現 insertColumns () 在子類,必須調用此函數 after 把數據插入模型的底層數據存儲。

另請參閱 beginInsertColumns ().

[protected] void QAbstractItemModel:: endInsertRows ()

結束行插入操作。

當重實現 insertRows () 在子類,必須調用此函數 after 把數據插入模型的底層數據存儲。

另請參閱 beginInsertRows ().

[protected] void QAbstractItemModel:: endMoveColumns ()

結束列移動操作。

當實現子類時,必須調用此函數 after 於模型的底層數據存儲中移動數據。

該函數在 Qt 4.6 引入。

另請參閱 beginMoveColumns ().

[protected] void QAbstractItemModel:: endMoveRows ()

結束行移動操作。

當實現子類時,必須調用此函數 after 於模型的底層數據存儲中移動數據。

該函數在 Qt 4.6 引入。

另請參閱 beginMoveRows ().

[protected] void QAbstractItemModel:: endRemoveColumns ()

結束列移除操作。

當重實現 removeColumns () 在子類,必須調用此函數 after 移除數據從模型的底層數據存儲。

另請參閱 beginRemoveColumns ().

[protected] void QAbstractItemModel:: endRemoveRows ()

結束行移除操作。

當重實現 removeRows () 在子類,必須調用此函數 after 移除數據從模型的底層數據存儲。

另請參閱 beginRemoveRows ().

[protected] void QAbstractItemModel:: endResetModel ()

完成模型重置操作。

You must call this function after resetting any internal data structure in your model or proxy model.

此函數發射信號 modelReset ().

該函數在 Qt 4.6 引入。

另請參閱 beginResetModel ().

[虛擬] void QAbstractItemModel:: fetchMore (const QModelIndex & parent )

抓取針對項的任何可用數據,采用父級指定通過 parent 索引。

重實現此,若正增量填充模型。

默認實現什麼都不做。

注意: 此函數可以被援引,通過元對象係統和從 QML。見 Q_INVOKABLE .

另請參閱 canFetchMore ().

[虛擬] Qt::ItemFlags QAbstractItemModel:: flags (const QModelIndex & index ) const

返迴項標誌為給定 index .

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

注意: 此函數可以被援引,通過元對象係統和從 QML。見 Q_INVOKABLE .

另請參閱 Qt::ItemFlags .

[虛擬] bool QAbstractItemModel:: hasChildren (const QModelIndex & parent = QModelIndex()) const

返迴 true if parent 有任何子級;否則返迴 false .

使用 rowCount () on the parent to find out the number of children.

Note that it is undefined behavior to report that a particular index hasChildren with this method if the same index has the flag Qt::ItemNeverHasChildren set.

注意: 此函數可以被援引,通過元對象係統和從 QML。見 Q_INVOKABLE .

另請參閱 parent () 和 index ().

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

返迴 true 若模型返迴有效 QModelIndex for row and column with parent ,否則返迴 false .

注意: 此函數可以被援引,通過元對象係統和從 QML。見 Q_INVOKABLE .

[虛擬] QVariant QAbstractItemModel:: headerData ( int section , Qt::Orientation orientation , int role = Qt::DisplayRole) const

返迴數據為給定 role and section 在 Header (頭) 采用指定 orientation .

對於水平 Header (頭),區間號相當於列號。同樣,對於垂直 Header (頭),區間號相當於行號。

注意: 此函數可以被援引,通過元對象係統和從 QML。見 Q_INVOKABLE .

另請參閱 Qt::ItemDataRole , setHeaderData (),和 QHeaderView .

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

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 last need to be updated.

當重實現 setHeaderData () 函數,必須明確發射此信號。

If you are changing the number of columns or rows you do not need to emit this signal, but use the begin/end functions (refer to the section on subclassing in the QAbstractItemModel 類描述瞭解細節)。

另請參閱 headerData (), setHeaderData (),和 dataChanged ().

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

返迴模型中的項索引,指定通過給定 row , column and parent 索引。

當在子類中重實現此函數時,調用 createIndex () 去生成模型索引,其它組件可以使用以引用模型中的項。

注意: 此函數可以被援引,通過元對象係統和從 QML。見 Q_INVOKABLE .

另請參閱 createIndex ().

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

插入單列先於給定 column in the child items of the parent 指定。

返迴 true 若列被插入;否則返迴 false .

另請參閱 insertColumns (), insertRow (),和 removeColumn ().

[虛擬] bool QAbstractItemModel:: insertColumns ( int column , int count , const QModelIndex & parent = QModelIndex())

對於支持這的模型,插入 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 模型索引。

column is 0, the columns are prepended to any existing columns.

column is columnCount (), the columns are appended to any existing columns.

parent has no children, a single row with count columns is inserted.

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

基類實現什麼都不做,並返迴 false .

If you implement your own model, you can reimplement this function if you want to support insertions. Alternatively, you can provide your own API for altering the data.

另請參閱 insertRows (), removeColumns (), beginInsertColumns (),和 endInsertColumns ().

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

插入單行先於給定 row in the child items of the parent 指定。

注意: 此函數調用虛擬方法 insertRows .

返迴 true 若行被插入;否則返迴 false .

另請參閱 insertRows (), insertColumn (),和 removeRow ().

[虛擬] bool QAbstractItemModel:: insertRows ( int row , int count , const QModelIndex & parent = QModelIndex())

注意: The base class implementation of this function does nothing and returns false .

對於支持這的模型,插入 count rows into the model before the given row . Items in the new row will be children of the item represented by the parent 模型索引。

row is 0, the rows are prepended to any existing rows in the parent.

row is rowCount (), the rows are appended to any existing rows in the parent.

parent has no children, a single column with count rows is inserted.

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

If you implement your own model, you can reimplement this function if you want to support insertions. Alternatively, you can provide your own API for altering the data. In either case, you will need to call beginInsertRows () 和 endInsertRows () to notify other components that the model has changed.

另請參閱 insertColumns (), removeRows (), beginInsertRows (),和 endInsertRows ().

[虛擬] QMap < int , QVariant > QAbstractItemModel:: itemData (const QModelIndex & index ) const

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

Reimplement this function if you want to extend the default behavior of this function to include custom roles in the map.

另請參閱 setItemData (), Qt::ItemDataRole ,和 data ().

[signal] void QAbstractItemModel:: layoutAboutToBeChanged (const QList < QPersistentModelIndex > & parents = ..., QAbstractItemModel::LayoutChangeHint hint = ...)

This signal is emitted just before the layout of a model is changed. Components connected to this signal use it to adapt to changes in the model's layout.

Subclasses should update any persistent model indexes after emitting layoutAboutToBeChanged().

可選 parents parameter is used to give a more specific notification about what parts of the layout of the model are changing. An empty list indicates a change to the layout of the entire model. The order of elements in the parents list is not significant. The optional hint parameter is used to give a hint about what is happening while the model is relayouting.

該函數在 Qt 5.0 引入。

另請參閱 layoutChanged () 和 changePersistentIndex ().

[signal] void QAbstractItemModel:: layoutChanged (const QList < QPersistentModelIndex > & parents = ..., QAbstractItemModel::LayoutChangeHint hint = ...)

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

當子類化 QAbstractItemModel or QAbstractProxyModel ,確保發射 layoutAboutToBeChanged () before changing the order of items or altering the structure of the data you expose to views, and emit layoutChanged() after changing the layout.

可選 parents parameter is used to give a more specific notification about what parts of the layout of the model are changing. An empty list indicates a change to the layout of the entire model. The order of elements in the parents list is not significant. The optional hint parameter is used to give a hint about what is happening while the model is relayouting.

Subclasses should update any persistent model indexes before emitting layoutChanged(). In other words, when the structure changes:

該函數在 Qt 5.0 引入。

另請參閱 layoutAboutToBeChanged (), dataChanged (), headerDataChanged (), modelReset (),和 changePersistentIndex ().

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

Returns a list of indexes for the items in the column of the start index where 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. Note also that the order of results in the list may not correspond to the order in the model, if for example a proxy model is used. The order of the results can not be relied upon.

The search begins 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 指定在 flags . If you want to search for all matching items, use hits = -1.

By default, this function will perform a wrapping, string-based comparison on all items, searching for items that begin with the search term specified by value .

注意: The default implementation of this function only searches columns. Reimplement this function to include a different search behavior.

注意: 此函數可以被援引,通過元對象係統和從 QML。見 Q_INVOKABLE .

[虛擬] QMimeData *QAbstractItemModel:: mimeData (const QModelIndexList & indexes ) const

Returns an object that contains serialized items of data corresponding to the list of indexes specified. The format used to describe the encoded data is obtained from the mimeTypes () function. This default implementation uses the default MIME type returned by the default implementation of mimeTypes (). If you reimplement mimeTypes () in your custom model to return more MIME types, reimplement this function to make use of them.

If the list of indexes is empty, or there are no supported MIME types, 0 is returned rather than a serialized empty list.

另請參閱 mimeTypes () 和 dropMimeData ().

[虛擬] QStringList QAbstractItemModel:: mimeTypes () const

Returns the list of allowed MIME types. By default, the built-in models and views use an internal MIME type: application/x-qabstractitemmodeldatalist .

When implementing drag and drop support in a custom model, if you will return data in formats other than the default internal MIME type, reimplement this function to return your list of MIME types.

If you reimplement this function in your custom model, you must also reimplement the member functions that call it: mimeData () 和 dropMimeData ().

另請參閱 mimeData () 和 dropMimeData ().

[signal] void QAbstractItemModel:: modelAboutToBeReset ()

此信號發射,當 beginResetModel () is called, before the model's internal state (e.g. persistent model indexes) has been invalidated.

該函數在 Qt 4.2 引入。

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 beginResetModel () 和 modelReset ().

[signal] void QAbstractItemModel:: modelReset ()

此信號發射,當 endResetModel () is called, after the model's internal state (e.g. persistent model indexes) has been invalidated.

Note that if a model is reset it should be considered that all information previously retrieved from it is invalid. This includes but is not limited to the rowCount () 和 columnCount (), flags (), data retrieved through data (),和 roleNames ().

該函數在 Qt 4.1 引入。

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 endResetModel () 和 modelAboutToBeReset ().

bool QAbstractItemModel:: moveColumn (const QModelIndex & sourceParent , int sourceColumn , const QModelIndex & destinationParent , int destinationChild )

對於支持這的模型,移動 sourceColumn from sourceParent to destinationChild under destinationParent .

返迴 true 若成功移動列;否則返迴 false .

另請參閱 moveColumns () 和 moveRow ().

[虛擬] bool QAbstractItemModel:: moveColumns (const QModelIndex & sourceParent , int sourceColumn , int count , const QModelIndex & destinationParent , int destinationChild )

對於支持這的模型,移動 count 列始於給定 sourceColumn 在父級 sourceParent to column destinationChild 在父級 destinationParent .

返迴 true 若成功移動列;否則返迴 false .

基類實現什麼都不做,並返迴 false .

若實現自己的模型,可以重實現此函數,若想要支持移動。另外,為更改數據可以提供自己的 API。

另請參閱 beginMoveColumns () 和 endMoveColumns ().

bool QAbstractItemModel:: moveRow (const QModelIndex & sourceParent , int sourceRow , const QModelIndex & destinationParent , int destinationChild )

對於支持這的模型,移動 sourceRow from sourceParent to destinationChild under destinationParent .

返迴 true 若成功移動行;否則返迴 false .

另請參閱 moveRows () 和 moveColumn ().

[虛擬] bool QAbstractItemModel:: moveRows (const QModelIndex & sourceParent , int sourceRow , int count , const QModelIndex & destinationParent , int destinationChild )

對於支持這的模型,移動 count 行始於給定 sourceRow 在父級 sourceParent to row destinationChild 在父級 destinationParent .

返迴 true 若成功移動行;否則返迴 false .

基類實現什麼都不做,並返迴 false .

若實現自己的模型,可以重實現此函數,若想要支持移動。另外,為更改數據可以提供自己的 API。

另請參閱 beginMoveRows () 和 endMoveRows ().

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

返迴模型項的父級采用給定 index 。若項沒有父級,無效 QModelIndex 被返迴。

A common convention used in models that expose tree data structures is that only items in the first column have children. For that case, when reimplementing this function in a subclass the column of the returned QModelIndex would be 0.

在子類重實現此函數時,小心避免調用 QModelIndex member functions, such as QModelIndex::parent (), since indexes belonging to your model will simply call your implementation, leading to infinite recursion.

注意: 此函數可以被援引,通過元對象係統和從 QML。見 Q_INVOKABLE .

另請參閱 createIndex ().

[protected] QModelIndexList QAbstractItemModel:: persistentIndexList () const

返迴以持久索引方式,存儲在模型中的索引列錶。

該函數在 Qt 4.2 引入。

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

移除給定 column 從子級項對於 parent 指定。

返迴 true 若列被移除;否則返迴 false .

另請參閱 removeColumns (), removeRow (),和 insertColumn ().

[虛擬] bool QAbstractItemModel:: removeColumns ( int column , int count , const QModelIndex & parent = QModelIndex())

對於支持這的模型,移除 count 列始於給定 column 在父級 parent 來自模型。

返迴 true 若列被成功移除;否則返迴 false .

基類實現什麼都不做,並返迴 false .

If you implement your own model, you can reimplement this function if you want to support removing. Alternatively, you can provide your own API for altering the data.

另請參閱 removeColumn (), removeRows (), insertColumns (), beginRemoveColumns (),和 endRemoveColumns ().

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

移除給定 row 從子級項對於 parent 指定。

返迴 true 若行被移除;否則返迴 false .

這是方便函數當調用 removeRows ()。 QAbstractItemModel 實現的 removeRows () 什麼都不做。

另請參閱 removeRows (), removeColumn (),和 insertRow ().

[虛擬] bool QAbstractItemModel:: removeRows ( int row , int count , const QModelIndex & parent = QModelIndex())

對於支持這的模型,移除 count 行始於給定 row 在父級 parent 來自模型。

返迴 true 若成功移除行;否則返迴 false .

基類實現什麼都不做,並返迴 false .

If you implement your own model, you can reimplement this function if you want to support removing. Alternatively, you can provide your own API for altering the data.

另請參閱 removeRow (), removeColumns (), insertColumns (), beginRemoveRows (),和 endRemoveRows ().

[protected slot] void QAbstractItemModel:: resetInternalData ()

This slot is called just after the internal data of a model is cleared while it is being reset.

This slot is provided the convenience of subclasses of concrete proxy models, such as subclasses of QSortFilterProxyModel 維護額外數據。

class CustomDataProxy : public QSortFilterProxyModel
{
    Q_OBJECT
public:
    CustomDataProxy(QObject *parent)
      : QSortFilterProxyModel(parent)
    {
    }
    ...
    QVariant data(const QModelIndex &index, int role) override
    {
        if (role != Qt::BackgroundRole)
            return QSortFilterProxyModel::data(index, role);
        if (m_customData.contains(index.row()))
            return m_customData.value(index.row());
        return QSortFilterProxyModel::data(index, role);
    }
private slots:
    void resetInternalData()
    {
        m_customData.clear();
    }
private:
  QHash<int, QVariant> m_customData;
};
					

注意: 由於過失,此槽在 Qt 5.0 缺失。

該函數在 Qt 4.8 引入。

另請參閱 modelAboutToBeReset () 和 modelReset ().

[virtual slot] void QAbstractItemModel:: revert ()

Lets the model know that it should discard cached information. This function is typically used for row editing.

另請參閱 submit ().

[虛擬] QHash < int , QByteArray > QAbstractItemModel:: roleNames () const

返迴模型的角色名稱。

由 Qt 設置的默認角色名,包括:

Qt 角色 QML 角色名稱
Qt::DisplayRole display
Qt::DecorationRole decoration
Qt::EditRole edit
Qt::ToolTipRole toolTip
Qt::StatusTipRole statusTip
Qt::WhatsThisRole whatsThis

該函數在 Qt 4.6 引入。

另請參閱 setRoleNames ().

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

Returns the number of rows under the given parent . When the parent is valid it means that rowCount is returning the number of children of parent.

注意: When implementing a table based model, rowCount() should return 0 when the parent is valid.

注意: 此函數可以被援引,通過元對象係統和從 QML。見 Q_INVOKABLE .

另請參閱 columnCount ().

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

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 項。

注意: Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 insertRows () 和 beginInsertRows ().

[signal] void QAbstractItemModel:: rowsAboutToBeMoved (const QModelIndex & sourceParent , int sourceStart , int sourceEnd , const QModelIndex & destinationParent , int destinationRow )

This signal is emitted just before rows are moved within the model. The items that will be moved are those between sourceStart and sourceEnd inclusive, under the given sourceParent item. They will be moved to destinationParent starting at the row destinationRow .

注意: Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.

該函數在 Qt 4.6 引入。

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 beginMoveRows ().

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

This signal is emitted just before rows are removed from the model. The items that will be removed are those between first and last inclusive, under the given parent 項。

注意: Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 removeRows () 和 beginRemoveRows ().

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

This signal is emitted after rows have been inserted into the model. The new items are those between first and last inclusive, under the given parent 項。

注意: Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 insertRows () 和 beginInsertRows ().

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

This signal is emitted after rows have been moved within the model. The items between start and end inclusive, under the given parent item have been moved to destination starting at the row row .

注意: Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.

該函數在 Qt 4.6 引入。

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 beginMoveRows ().

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

This signal is emitted after rows have been removed from the model. The removed items are those between first and last inclusive, under the given parent 項。

注意: Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 removeRows () 和 beginRemoveRows ().

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

設置 role 數據為項在 index to value .

返迴 true 若成功;否則返迴 false .

The dataChanged () 信號應該被發射,若數據設置成功。

基類實現返迴 false 。此函數和 data () 必須被重實現為可編輯模型。

注意: 此函數可以被援引,通過元對象係統和從 QML。見 Q_INVOKABLE .

另請參閱 Qt::ItemDataRole , data (),和 itemData ().

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

設置數據為給定 role and section 在 Header (頭) 采用指定 orientation value 供給。

返迴 true 若 Header (頭部) 數據被更新;否則返迴 false .

當重實現此函數時, headerDataChanged () 信號必須明確被發射。

另請參閱 Qt::ItemDataRole and headerData ().

[虛擬] bool QAbstractItemModel:: setItemData (const QModelIndex & index , const QMap < int , QVariant > & roles )

Sets the role data for the item at index to the associated value in roles , for every Qt::ItemDataRole .

返迴 true 若成功;否則返迴 false .

Roles that are not in roles will not be modified.

另請參閱 setData (), data (),和 itemData ().

[虛擬] QModelIndex QAbstractItemModel:: sibling ( int row , int column , const QModelIndex & index ) const

返迴同級在 row and column for the item at index ,或無效 QModelIndex if there is no sibling at that location.

sibling() is just a convenience function that finds the item's parent, and uses it to retrieve the index of the child item in the specified row and column .

This method can optionally be overridden for implementation-specific optimization.

注意: 此函數可以被援引,通過元對象係統和從 QML。見 Q_INVOKABLE .

另請參閱 index (), QModelIndex::row (),和 QModelIndex::column ().

[虛擬] void QAbstractItemModel:: sort ( int column , Qt::SortOrder order = Qt::AscendingOrder)

排序模型按 column 以給定 order .

基類實現什麼都不做。

[虛擬] QSize QAbstractItemModel:: span (const QModelIndex & index ) const

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

注意: Currently, span is not used.

[virtual slot] bool QAbstractItemModel:: submit ()

Lets the model know that it should submit cached information to permanent storage. This function is typically used for row editing.

返迴 true 若沒有錯誤;否則返迴 false .

另請參閱 revert ().

[虛擬] Qt::DropActions QAbstractItemModel:: supportedDragActions () const

Returns the actions supported by the data in this model.

默認實現返迴 supportedDropActions (). Reimplement this function if you wish to support additional actions.

supportedDragActions() is used by QAbstractItemView::startDrag () as the default values when a drag occurs.

另請參閱 setSupportedDragActions (), Qt::DropActions ,和 將拖放用於項視圖 .

[虛擬] Qt::DropActions QAbstractItemModel:: supportedDropActions () const

返迴由此模型支持的掉落動作。

默認實現返迴 Qt::CopyAction . Reimplement this function if you wish to support additional actions. You must also reimplement the dropMimeData () function to handle the additional operations.

該函數在 Qt 4.2 引入。

另請參閱 dropMimeData (), Qt::DropActions ,和 將拖放用於項視圖 .