QModelIndex 類

QModelIndex 類用於在數據模型中定位數據。 更多...

頭: #include <QModelIndex>
qmake: QT += core

公共函數

QModelIndex ()
int column () const
QVariant data (int role = Qt::DisplayRole) const
Qt::ItemFlags flags () const
quintptr internalId () const
void * internalPointer () const
bool isValid () const
const QAbstractItemModel * model () const
QModelIndex parent () const
int row () const
QModelIndex sibling (int row , int column ) const
QModelIndex siblingAtColumn (int column ) const
QModelIndex siblingAtRow (int row ) const
bool operator!= (const QModelIndex & other ) const
bool operator< (const QModelIndex & other ) const
bool operator== (const QModelIndex & other ) const
typedef QModelIndexList

詳細描述

此類用作項模型的索引,派生自 QAbstractItemModel 。用於項視圖、委托及選定模型的索引,能定位模型項。

通過模型創建新 QModelIndex 對象是使用 QAbstractItemModel::createIndex () 函數。 無效 模型索引的構造可以采用 QModelIndex 構造函數。無效索引經常用作父級索引,當引用模型中的頂級項時。

模型索引引用模型中的項,且包含指定它們在這些模型中位置要求的所有信息。各索引位於給定行和列,且可能擁有父級索引;使用 row (), column (),和 parent () 能獲得這種信息。模型中的各頂級項都是由不擁有父級索引的模型索引所錶示的 - 在此情況下, parent () 將返迴無效模型索引,相當於采用 QModelIndex() 構造函數的 0 自變量形式構造索引。

要獲得引用模型中現有項的模型索引,調用 QAbstractItemModel::index () 采用要求的行和列值,及父級的模型索引。當引用模型中的頂級項時,提供 QModelIndex() 作為父級索引。

The model () 函數返迴索引所引用的模型如 QAbstractItemModel 。child() 函數用於審查由模型索引所保持的項。 sibling () 函數允許在如索引的同一級彆遍曆模型項。

注意: 模型索引應立即使用,然後丟棄。不應依賴要保持有效的索引,在調用改變模型結構 (或刪除項) 的模型函數後。若需要隨時間推移保持模型索引,使用 QPersistentModelIndex .

另請參閱 模型/視圖編程 , QPersistentModelIndex ,和 QAbstractItemModel .

成員函數文檔編製

QModelIndex:: QModelIndex ()

創建新的空模型索引。這種類型的模型索引用於指示模型的位置無效。

另請參閱 isValid () 和 QAbstractItemModel .

int QModelIndex:: column () const

返迴此模型索引所引用的列。

QVariant QModelIndex:: data ( int role = Qt::DisplayRole) const

返迴數據為給定 role 對於項引用通過索引。

Qt::ItemFlags QModelIndex:: flags () const

返迴由索引所引用的項標誌。

該函數在 Qt 4.2 引入。

quintptr QModelIndex:: internalId () const

返迴 quintptr 用於模型,以將索引關聯內部數據結構。

另請參閱 QAbstractItemModel::createIndex ().

void *QModelIndex:: internalPointer () const

返迴 void * 指針用於模型,以將索引關聯內部數據結構。

另請參閱 QAbstractItemModel::createIndex ().

bool QModelIndex:: isValid () const

返迴 true 若此模型索引有效;否則返迴 false .

有效索引屬於模型,且擁有非負行號和列號。

另請參閱 model (), row (),和 column ().

const QAbstractItemModel *QModelIndex:: model () const

返迴包含此索引所引用項的模型指針。

返迴模型 const 指針,因為調用模型的非 const 函數會使模型索引無效,並可能會使應用程序崩潰。

QModelIndex QModelIndex:: parent () const

返迴模型索引的父級,或 QModelIndex () 若它沒有父級。

另請參閱 sibling () 和 model ().

int QModelIndex:: row () const

返迴此模型索引所引用的行。

QModelIndex QModelIndex:: sibling ( int row , int column ) const

返迴同級在 row and column 。若此位置沒有同級,無效 QModelIndex 被返迴。

另請參閱 parent (), siblingAtColumn (),和 siblingAtRow ().

QModelIndex QModelIndex:: siblingAtColumn ( int column ) const

返迴同級在 column 對於當前行。若此位置沒有同級,無效 QModelIndex 被返迴。

該函數在 Qt 5.11 引入。

另請參閱 sibling () 和 siblingAtRow ().

QModelIndex QModelIndex:: siblingAtRow ( int row ) const

返迴同級在 row 對於當前列。若此位置沒有同級,無效 QModelIndex 被返迴。

該函數在 Qt 5.11 引入。

另請參閱 sibling () 和 siblingAtColumn ().

bool QModelIndex:: operator!= (const QModelIndex & other ) const

返迴 true 若此模型索引未引用同一位置如 other 模型索引;否則返迴 false .

bool QModelIndex:: operator< (const QModelIndex & other ) const

返迴 true 若此模型索引 < other 模型索引;否則返迴 false .

< 計算對開發者並非直接有用 - 沒有定義采用不同父級的索引比較方式。此運算符纔存在,以便類可以用於 QMap .

該函數在 Qt 4.1 引入。

bool QModelIndex:: operator== (const QModelIndex & other ) const

返迴 true 若此模型索引引用同一位置如 other 模型索引;否則返迴 false .

使用內部數據指針、行、列及模型值,當比較另一模型索引時。

相關非成員

typedef QModelIndexList

同義詞 QList < QModelIndex >.