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