Qt mvc学习一
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
?mvc是經(jīng)典的三層結(jié)構(gòu),將數(shù)據(jù),視圖和邏輯分離。Qt中的Model/View框架,實(shí)現(xiàn)了這個(gè)模式。在Qt中這個(gè)模式設(shè)計(jì)到三個(gè)類,model類,view類和delegate類。model類保存數(shù)據(jù),view復(fù)制顯示,而delegate負(fù)責(zé)協(xié)調(diào)model和view之間的數(shù)據(jù)edit(編輯)和render(渲染)。
??????????????????
這些在model子類中需要實(shí)現(xiàn)的方法可以分為三組。
項(xiàng)數(shù)據(jù)綁定:所有的model需要實(shí)現(xiàn)方法使視圖和代理能夠查詢model...
Models能夠提供各種程度的數(shù)據(jù)訪問限制:read-only,resizing,edited
Read-Only?access?只讀訪問
如果只讀訪問,只需要實(shí)現(xiàn)下面幾個(gè)函數(shù)在繼承的子類中
Flags,其他的組件可以通過這個(gè)得知每個(gè)Item的信息,在大多數(shù)的models中,包含Qt::ItemIsEnable,Qt::ItemIsSelectable
data,被用來提供數(shù)據(jù)給視圖和代理,一般的,models只要提供Qt::DisplayRole和任何程序特殊的角色,也有一些特殊的Qt::ToolTipRole等,詳細(xì)可以看Qt::ItemDataRole。
headerData,為視圖的頭部提供信息數(shù)據(jù)。
rowCount提供這個(gè)model有多少行數(shù)據(jù)。
上述的四個(gè)函數(shù)在任何類型的model中都要實(shí)現(xiàn),不管是QAbstractListModel還是QAbstractTableModel。另外,下面的函數(shù)必須被實(shí)現(xiàn),在QAbstractTableModel和QAbstractItemModel中,columnCount。
編輯項(xiàng)目
可編輯的模型允許數(shù)據(jù)項(xiàng)被修改,和可以提供函數(shù)來插入數(shù)據(jù)在行和列。
Flags,必須包含Qt::ItemDataRole。
setData,被用來修改和特殊的模型索引相關(guān)的項(xiàng)目。修改的數(shù)據(jù)必須是Qt::EditRole,發(fā)送一個(gè)dataChanged信號(hào)。
setHeaderData,用來修改水平和垂直的頭信息,發(fā)出一個(gè)headerDataChanged信號(hào)。
改變models的size
所有類型的model能夠提供插入和移除行。Table?Model和分級的model也支持列的插入和刪除操作。
下面的例子是基于QAbstractListModel實(shí)現(xiàn)的一個(gè)QStringListModel
/************************************************ * *author:周翔 *e-mail:604487178@qq.com *blog:http://blog.csdn.net/zhx6044 * * *************************************************/#ifndef STRINGLISTMODEL_HPP #define STRINGLISTMODEL_HPP#include <QAbstractListModel> #include <QStringList>class StringListModel : public QAbstractListModel {Q_OBJECT public:explicit StringListModel( const QStringList &stringList, QObject *parent = 0);//重新實(shí)現(xiàn)的函數(shù)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); signals:public slots: private:QStringList m_slist;//存放數(shù)據(jù)的容器};#endif // STRINGLISTMODEL_HPP/************************************************ * *author:周翔 *e-mail:604487178@qq.com *blog:http://blog.csdn.net/zhx6044 * * *************************************************/#include "stringlistmodel.hpp" #include <QDebug>StringListModel::StringListModel(const QStringList &stringList, QObject *parent) :QAbstractListModel(parent),m_slist(stringList) { } /*** @brief StringListModel::rowCount model數(shù)據(jù)的行數(shù)* @return */ int StringListModel::rowCount(const QModelIndex &/*parent*/) const {return m_slist.length();//就是鏈表的長度 } /*** @brief StringListModel::data 獲得對應(yīng)index項(xiàng)的數(shù)據(jù)* @param index* @param role 數(shù)據(jù)的角色* @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("col %1").arg(section);} else {return QString("row %1").arg(section);} } /*** @brief StringListModel::flags 被其他組件訪問時(shí)獲得每個(gè)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 當(dāng)視圖的顯示的數(shù)據(jù)被改變的時(shí)候,model也相應(yīng)的改變* @param index* @param value* @param role* @return */ bool StringListModel::setData(const QModelIndex &index, const QVariant &value, int role) {//這個(gè)index必須是有效的,必須還是可編輯的if (index.isValid() && role == Qt::EditRole) {m_slist.replace(index.row(),value.toString());emit dataChanged(index,index);//發(fā)出這個(gè)信號(hào),外部使用這個(gè)信號(hào)沒用return true;}return false;}
使用這個(gè)model類
修改數(shù)據(jù)項(xiàng)
轉(zhuǎn)載于:https://my.oschina.net/u/854744/blog/418530
總結(jié)
- 上一篇: 八、数据库服务连接存储(MPIO) 中
- 下一篇: QTP自动化测试自学手册V2.0版本