清源游民 gameogre@gmail.com
創(chuàng)建新的Models
介紹
model/view組件之間功能的分離,允許創(chuàng)建model利用現(xiàn)成的views。這也可以使用標(biāo)準(zhǔn)的功能 圖形用戶接口組件像QListView,QTableView和QTreeView來(lái)顯示來(lái)自各種數(shù)據(jù)源的數(shù)據(jù)為。
QAbstractListModel類提供了非常靈活的接口,允許數(shù)據(jù)源以層次結(jié)構(gòu)的形式來(lái)管理信息,也允許以某種
方式對(duì)數(shù)據(jù)進(jìn)行插入、刪除、修改和存儲(chǔ)。它也提供了對(duì)拖拽操作的支持。
QAbstractListModel與QAbstractTableModel為簡(jiǎn)單的非層次結(jié)構(gòu)的數(shù)據(jù)提供了接口,對(duì)于比較簡(jiǎn)單的list和table models來(lái)說(shuō),這是不錯(cuò)的一個(gè)開(kāi)始點(diǎn)。
設(shè)計(jì)一個(gè)Model
當(dāng)我們?yōu)榇嬖诘臄?shù)據(jù)結(jié)構(gòu)新建一個(gè)model時(shí),首先要考慮的問(wèn)題是應(yīng)該選用哪種model來(lái)為這些數(shù)據(jù)提供接口。
假如數(shù)據(jù)結(jié)構(gòu)可以用數(shù)據(jù)項(xiàng)的列表或表來(lái)表示,那么可以考慮子類化QAbstractListModel或QAbstractTableModel
,既然這些類已經(jīng)合理地對(duì)許多功能提供缺省實(shí)現(xiàn)。
然而,假如底層的數(shù)據(jù)結(jié)構(gòu)只能表示成具有層次結(jié)構(gòu)的樹(shù)型結(jié)構(gòu),那么必須得子類化QAbstractItemModel。
無(wú)論底層的數(shù)據(jù)結(jié)構(gòu)采取何種形式,在特定的model中實(shí)現(xiàn)標(biāo)準(zhǔn)的QAbstractItemModel API總是一個(gè)不錯(cuò)的主意,這使得可以使用更自然的方式對(duì)底層的數(shù)據(jù)結(jié)構(gòu)進(jìn)行訪問(wèn)。這也使得用數(shù)據(jù)構(gòu)建model 更為容易,其他
的model/view組件也可以使用標(biāo)準(zhǔn)的API與之進(jìn)行交互。
一個(gè)只讀model示例
這個(gè)示例實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的,非層次結(jié)構(gòu)的,只讀的數(shù)據(jù)model,它基于QStringistModel類。它有一個(gè)QStringList作為它內(nèi)部的數(shù)據(jù)源,只實(shí)現(xiàn)了一些必要的接口。為了簡(jiǎn)單化,它子類化了QAbstractListModel,這個(gè)基類提供了合理的缺省行為,對(duì)外提供了比QAbstractItemModel更為簡(jiǎn)單的接口。當(dāng)我們實(shí)現(xiàn)一個(gè)model時(shí),不要忘了QAbstractItemModel本身不存儲(chǔ)任何數(shù)據(jù),它僅僅提供了給views訪問(wèn)
數(shù)據(jù)的接口。
class StringListModel : public QAbstractListModel
{
Q_OBJECT
public:
StringListModel(const QStringList &strings, QObject *parent = 0)
: QAbstractListModel(parent), stringList(strings) {}
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const;
private:
QStringList stringList;
};
除了構(gòu)造函數(shù),我們僅需要實(shí)現(xiàn)兩個(gè)函數(shù):rowCount()返回model中的行數(shù),data()返回與特定model index對(duì)應(yīng)的數(shù)據(jù)項(xiàng)。具有良好行為的model也會(huì)實(shí)現(xiàn)headerData(),它返回tree和table views需要的,在標(biāo)題中顯示的數(shù)據(jù)。
因?yàn)檫@是一個(gè)非層次結(jié)構(gòu)的model,我們不必考慮父子關(guān)系。假如model具有層次結(jié)構(gòu),我們也應(yīng)該實(shí)現(xiàn)index()與parent()函數(shù)。
Model的尺寸
我們認(rèn)為model中的行數(shù)與string list中的string數(shù)目一致:
int StringListModel::rowCount(const QModelIndex &parent) const
{
return stringList.count();
}
在缺省情況下,從QAbstractListModel派生的model只具有一列,因此不需要實(shí)現(xiàn)columnCount()。
Model 標(biāo)題與數(shù)據(jù)
QVariant StringListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= stringList.size())
return QVariant();
if (role == Qt::DisplayRole)
return stringList.at(index.row());
else
return QVariant();
}
QVariant StringListModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal)
return QString("Column %1").arg(section);
else
return QString("Row %1").arg(section);
}
一個(gè)數(shù)據(jù)項(xiàng)可能有多個(gè)角色,根據(jù)角色的不同輸出不同的數(shù)據(jù)。上例中,model中的數(shù)據(jù)項(xiàng)只有一個(gè)角色 ,
DisplayRole,然而我們也可以重用提供給DisplayRole的數(shù)據(jù),作為別的角色使用,如我們可以作為ToolTipRole來(lái)用。
可編輯的model
上面我們演示了一個(gè)只讀的model,它只用于向用戶顯示,對(duì)于許多程序來(lái)說(shuō),可編輯的list model可能更有用。我們只需要給只讀的model提供另外兩個(gè)函數(shù)flags()與setData()的實(shí)現(xiàn)。下列函數(shù)聲明被添加到類定義中:
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole);
讓model可編輯
delegate會(huì)在創(chuàng)建編輯器之前檢查數(shù)據(jù)項(xiàng)是否是可編輯的。model必須得讓delegate知道它的數(shù)據(jù)項(xiàng)是可
編輯的。這可以通過(guò)為每一個(gè)數(shù)據(jù)項(xiàng)返回一個(gè)正確的標(biāo)記得到,在本例中,我們假設(shè)所有的數(shù)據(jù)項(xiàng)都是
可編輯可選擇的:
Qt::ItemFlags StringListModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
我們不必知道delegate執(zhí)行怎樣實(shí)際的編輯處理過(guò)程,我們只需提供給delegate一個(gè)方法,delegate會(huì)使用它對(duì)model中的數(shù)據(jù)進(jìn)行設(shè)置。這個(gè)特殊的函數(shù)就是setData():
bool StringListModel::setData(const QModelIndex &index,
const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
stringList.replace(index.row(), value.toString());
emit dataChanged(index, index);
return true;
}
return false;
}
當(dāng)數(shù)據(jù)被設(shè)置后,model必須得讓views知道一些數(shù)據(jù)發(fā)生了變化,這可通過(guò)發(fā)射一個(gè)dataChanged() 信號(hào)實(shí)現(xiàn)。
因?yàn)橹挥幸粋€(gè)數(shù)據(jù)項(xiàng)發(fā)生了變化,因此在信號(hào)中說(shuō)明的變化范圍只限于一個(gè)model index。
插入,刪除行
在model中改變行數(shù)與列數(shù)是可能的。當(dāng)然在本列中,只考慮行的情況,我們只需要重新實(shí)現(xiàn)插入、刪除
的函數(shù)就可以了,下面應(yīng)在類定義中聲明:
bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex());
bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex());
既然model中的每行對(duì)應(yīng)于列表中的一個(gè)string,因此,insertRows()函數(shù)在string list 中指定位置插入一個(gè)空string,
父index通常用于決定model中行列的位置,本例中只有一個(gè)單獨(dú)的頂級(jí)項(xiàng),困此只需要在list中插入空string。
bool StringListModel::insertRows(int position, int rows, const QModelIndex &parent)
{
beginInsertRows(QModelIndex(), position, position+rows-1);
for (int row = 0; row < rows; ++row) {
stringList.insert(position, "");
}
endInsertRows();
return true;
}
beginInsertRows()通知其他組件行數(shù)將會(huì)改變。endInsertRows()對(duì)操作進(jìn)行確認(rèn)與通知。
返回true表示成功。
刪除操作與插入操作類似:
bool StringListModel::removeRows(int position, int rows, const QModelIndex &parent)
{
beginRemoveRows(QModelIndex(), position, position+rows-1);
for (int row = 0; row < rows; ++row) {
stringList.removeAt(position);
}
endRemoveRows();
return true;
}
posted on 2007-06-18 11:56
清源游民 閱讀(8436)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
Qt