Model/View Framework中提供了模型model的抽象基類QAbstractItemModel, 如果需要自定義模型就需要繼承這個類并且實現(xiàn)一些必要的函數(shù)。 此外,Qt中又提供了QAbstractTableModel和QAbstractListModel分別繼承于上述基類,由名字可以清楚的知道這兩個類分別適用于表格模型和列表模型。對于這兩個模型來說,很多函數(shù)已經(jīng)重新實現(xiàn)過了,使用時直接繼承即可。
QAbstractItemModel QAbstractItemModel為元素模型類提供了抽象接口,自定義模型時需要繼承這個類。自定義的模型分為只讀模型和可編輯模型。
只讀模型:內(nèi)部數(shù)據(jù)不能修改 為了實現(xiàn)一個只讀模型,需要重新實現(xiàn)以下函數(shù):
flags
Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const這個函數(shù)返回被給模型索引index的標志,用于其他組件獲得每一個元素的信息。在很多模型中,返回的flags應該包括Qt::ItemIsEnabled和Qt::ItemIsSelectable,表示模型中的元素是可以被訪問和選擇的。通常實現(xiàn)的形式為:
if(!index.isValid()) return Qt::ItemIsEnabled;return QAbstractItemModel::flags(index) | Qt::ItemIsEnabled | Qt::ItemIsSelectable;data
QVariant QAbstractItemModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const這個函數(shù)返回模型索引index的底層數(shù)據(jù)(一個模型索引包括某一元素的信息,包括行,列,以及數(shù)據(jù)),用于視圖和委托訪問數(shù)據(jù)。通常的實現(xiàn)形式如下:
if(!index.isValid()) return QVariant();if(index.row() >= /*數(shù)據(jù)總個數(shù)*/) return QVariant();if(role == Qt::DisplayRole){ int row = index.row(); int column = index.column(); //根據(jù)模型存儲數(shù)據(jù)所用的數(shù)據(jù)結(jié)構(gòu)來返回對應行和列的數(shù)據(jù) return /*數(shù)據(jù)*/}return QVariant();headerData
QVariant QAbstractItemModel::headerData(int section, Qt::Orientation orientation, int role == Qt::DisplayRole) const這個函數(shù)返回某部分對應方向上的表頭,為Views提供顯示在Views頂部(即最上方和最左邊)的標識。通常實現(xiàn)形式為:
if(role != Qt::DisplayRole) return QVariant();if(orientation == Qt::Horizontal)//headersData在最上方,水平方向或if(orientation == Qt::Vertical) //headersData在最左邊,垂直方向{ //判斷section,section表示的是第幾個 return ...}return QVariant();rowCount
int QAbstractItemModel::rowCount(const QModelIndex &parent = QModelIndex()) const這個函數(shù)返回被給的模型索引下有多少行,返回的是parent的孩子數(shù)。而不是整個行數(shù)。如果沒有子元素,則返回0。
對于繼承于QAbstractListModel的類來說,只需要重新實現(xiàn)這四個函數(shù)。而對于繼承于QAbstractTableModel和QAbstractItemModel的類來說,除了上述四個函數(shù),還需要重新實現(xiàn)columnCount函數(shù),因為列表只有1列,而表格和樹則需要自定義:
columnCount
int QAbstractItemModel::columnCount(const QModelIndex &parent = QModelIndex()) const這個函數(shù)通常與給定的parent無關,所涉及的類有幾列就返回幾。
可編輯模型:允許數(shù)據(jù)被修改,也可以允許插入和刪除操作。
實現(xiàn)可編輯模型,除了上述只讀模型的函數(shù)外,還需要重新實現(xiàn)以下函數(shù):
flags
Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const這個函數(shù)和只讀模型的函數(shù)一樣,只是返回時需要增加Qt::ItemIsEditable,形式如下:
if(!index.isValid()) return Qt::ItemIsEnabled;return QAbstractItemModel::flags(index) | Qt::ItemIsEditabled;setData
bool QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole)data函數(shù)用來獲取數(shù)據(jù),setData函數(shù)用來設置模型索引index中存儲的數(shù)據(jù),形式如下:
if(index.isValid() && role == EditRole){ int row = index.row(); //根據(jù)行號從內(nèi)部數(shù)據(jù)結(jié)構(gòu)中定位,然后將其改編成value emit dataChanged(index, index); return true;}return false;另外需要注意的是,在改變完內(nèi)部數(shù)據(jù)后,需要發(fā)出dataChanged(index, index)信號通知Views和Delegate內(nèi)部數(shù)據(jù)已經(jīng)發(fā)生改變。
setHeaderData
bool QAbstractItemModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole)headerData函數(shù)用于返回表頭,這個函數(shù)設置表頭為value,形式如下:
if(role != EditRole) return false;if(orientation == Qt::Horizontal) 或 if(orientation == Qt::Vertical){ //改變存儲表頭的變量 emit headerDataChanged(orientation, section, section); return true;}return false;同樣,當改變表頭后,需要發(fā)出headerDataChanged(orientation, section, section)信號,通知Views和Delegate。
如果需要添加和刪除操作,則需要實現(xiàn)以下函數(shù): insertRows
bool QAbstractItemModel::insertRows(int position, int rows, const QModelIndex &index = QModelIndex())這個函數(shù)在父級為index的結(jié)構(gòu)中的第position行插入rows行。形式如下:
beginInsertRows(index, position, position + rows - 1);//內(nèi)部變量進行刪除操作endInsertRows();return true;進行插入操作時,前后分別需要調(diào)用beginInsertRows()和endInsertRows()函數(shù)。
removeRows
bool QAbstractItemModel::removeRows(int position, int rows, const QModelIndex &index = QModelIndex())這個函數(shù)在父級為index的結(jié)構(gòu)中的第position行刪除rows行。形式如下:
beginRemoveRows(index, position, position + rows - 1);//內(nèi)部變量進行刪除操作endRemoveRows();return true;進行刪除操作時,前后需要調(diào)用beginRemoveRows()和endRemoveRows()函數(shù)。
insertColumns removeColumns 這兩個函數(shù)同插入行和刪除行類似,也需要調(diào)用兩個函數(shù)。
通常情況下,函數(shù)應該返回true如果操作成功,但是如果只是部分操作成功,例如只插入了部分行,則需要返回false。
新聞熱點
疑難解答