Qt Creator:MVC学习

M(model)V(view)C(control)

Mvc是经典的三层结构,将结构、视图和逻辑分离。Qt中的Model/View框架,实现了这个模式。在Qt中这个模式设计到三个类,model类,view类和delegate类。model类保存数据,view类复制显示,而delegate负责协调model和view之间的数据edit(编辑)和render(渲染)。

这些在model子类中需要实现的方法可以分为三组。 项数据绑定:所有的model需要实现方法使视图和代理能够查询model…. Models能够提供各种程度的数据访问限制:read-only,resizing,edited

Read-Only access 只读访问 如果只读访问,只需要实现下面几个函数在继承的子类中 Flags,其他的组件可以通过这个得知每个Item的信息,在大多数models中,包含Qt::ItemIsEnable,Qt::ItemIsSelectabledata,被用来提供数据给视图和代理,一般的,models只提供Qt::DisplayRole和任何程序特殊的角色,也有一些特殊的Qt::ToolTipRole等,详细可以看Qt::ItemDataRole。 headerData,为视图的头部提供信息数据。 rowCount提供这个model有多少行数据。 上述的四个函数在任何类型的model中都要实现,不管是QAbstractListModel还是QAbstractTableModel。另外,下面的函数必须实现,在QAbstractTableModel和QAbstractItemModel中,columnCount。 编辑项目 可编辑的模型允许数据项被修改,和可以提供函数来插入数据在行和列。 Flags,必须包含Qt::ItemDataRole。 setData,被用来修改和特殊的模型索引相关的项目。修改的数据必须是Qt::EditRole,发送一个dataChanged信号。 setHeadeData,,用来修改水平和垂直的头信息,发出一个headerDataChanged信号。 改变models的size 所有类型的model能够提供插入和行移除。TableModel分级的model也支持列的插入和删除操作。

下面的例子是基于QAbstractListModel实现的一个QStringListModel

StringListModel.h

class StringListModel : public QAbstractListModel{Q_OBJECTpublic:explicit StringListModel(const QStringList &stringlist,QObject *parent = 0);public://重新实现的函数int rowCount(const QModelIndex &parent) const;QVariant data(const QModelIndex &index,int role) const;QVariant headerData(int section, Qt::Orientation orientation, int role) const;//实现可编辑函数Qt::ItemFlags flags(const QModelIndex &index) const;bool setData(const QModelIndex &index, const QVariant &value, int role);//插入,删除行bool insertRows(int row, int count, const QModelIndex &parent);bool removeRows(int row, int count, const QModelIndex &parent);//更改视图信息void addModel(const QString &c);signals:public slots:private:QStringList m_slist;//存放数据的容器(链表之类)};#endif // STRINGLISTMODEL_HPP

StringListModel.cpp

#include “StringListModel.hpp”StringListModel::StringListModel(const QStringList &,QObject *parent) :QAbstractListModel(parent){}int StringListModel::rowCount(const QModelIndex &) const{return m_slist.length();//返回链表的长度}/* * @brief StringListModel::data 获得对应index项的数据 * @param index * @param role 数据的角色 * @return */QVariant StringListModel::data(const QModelIndex &index, int role) const{if(!index.isValid()){return QVariant();}//row从0开始,有效的范围为0到链表长度-1if(index.row() >= m_slist.length()){return QVariant();}if(role == Qt::DisplayRole){return m_slist.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);}}/* * @brief StringListModel::flags 被其他组件访问时获得每个Item的信息 * @param index * @return */Qt::ItemFlags StringListModel::flags(const QModelIndex &index) const{if(!index.isValid()){return Qt::ItemIsEnabled;}return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;//可编辑的}/* * @brief StringListModel::setData 当视图的显示的数据被改变的时候,model也相应的改变 * @param index * @param value * @param role * @return */bool StringListModel::setData(const QModelIndex &index, const QVariant &value, int role){//这人index必须是有效的,必须还是可编辑的if(index.isValid() && role == Qt::EditRole){m_slist.replace(index.row(),value.toString());emit dataChanged(index,index);//发出这个信号,外部使用这个信号没用return true;}return false;}bool StringListModel::insertRows(int row, int count, const QModelIndex &){beginInsertRows(QModelIndex(), row, row + count -1);for(int i = 0;i < count ; ++i){m_slist.insert(row,QString());}endInsertRows();return true;}bool StringListModel::removeRows(int row, int count, const QModelIndex &){beginInsertRows(QModelIndex(),row,row + count-1);for(int i; i < count; ++i){m_slist.removeAt(row);}endRemoveRows();return true;}void StringListModel::addModel(const QString &c){//insertRows(rowCount(QModelIndex),1,QModelIndex());insertRow(rowCount(QModelIndex()));m_slist.replace(rowCount(QModelIndex())-1,c);}青春一经典当即永不再赎

Qt Creator:MVC学习

相关文章:

你感兴趣的文章:

标签云: