Qt MVC之自定义模型

来源:互联网 发布:suse linux 安装教程 编辑:程序博客网 时间:2024/05/18 13:27
        Qt中所有模型都是基于QAbstractItemModel派生的。当数据改变时,模型发出信号通知视图。为了保证数据的存取和表示分离,InterView
引入了模型索引(Model Index)的概念。
       通过模型索引来存取数据条目,必须有三个属性:行号,列号和父条目的模型索引。下面三种常见的模型索引组织方式:
          1.表模型:行和列
          2.列表模型:类似于表模型
          3.树模型:行,列和父索引。
      如果要实现自己的模型,可以从通用的QAbstractItemModel类继承,也可以从类QAbstractListModel和QAbstractTableModel继承实现列表

模型或表格模型。实现自定义模型必须要实现基类的纯虚函数和其他一些特定的函数。

weaponmodel.h文件

#ifndef WEAPONMODEL_H#define WEAPONMODEL_H#include <QAbstractTableModel>#include <QModelIndex>#include <QStringList>#include <QVariant>#include <QVector>class WeaponModel : public QAbstractTableModel{Q_OBJECTpublic:WeaponModel(QObject *parent=0);virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;virtual int columnCount(const QModelIndex &parent= QModelIndex()) const;QVariant data(const QModelIndex &index, int role) const;QVariant headerData(int section, Qt::Orientation orientation, int role) const;~WeaponModel();private:QVector<short> army;QVector<short> weapon;QStringList type;QMap<short,QString> armyMap;QMap<short,QString> weaponMap;QStringList header;void populateModel();};#endif // WEAPONMODEL_H
weaponmodel.cpp文件

#include "weaponmodel.h"#include <QDebug>WeaponModel::WeaponModel(QObject *parent): QAbstractTableModel(parent){armyMap[1]=tr("空军");armyMap[2]=tr("海军");armyMap[3]=tr("陆军");armyMap[4]=tr("海军陆战队");weaponMap[1]=tr("轰炸机");weaponMap[2]=tr("战斗机");weaponMap[3]=tr("航空母舰");weaponMap[4]=tr("驱逐舰");weaponMap[5]=tr("直升机");weaponMap[6]=tr("坦克");weaponMap[7]=tr("两栖攻击舰");weaponMap[8]=tr("两栖战车");populateModel();}WeaponModel::~WeaponModel(){}int WeaponModel::rowCount( const QModelIndex &parent /*= QModelIndex()*/ ) const{return army.size();}int WeaponModel::columnCount( const QModelIndex &parent/*= QModelIndex()*/ ) const{return 3;}QVariant WeaponModel::data( const QModelIndex &index, int role ) const{if(!index.isValid())return QVariant();qDebug()<<index;if(role==Qt::DisplayRole){switch(index.column()){case 0:return armyMap[army[index.row()]]; break;case 1:return weaponMap[weapon[index.row()]]; break;case 2:return type[index.row()]; default:return QVariant();}}return QVariant();}QVariant WeaponModel::headerData( int section, Qt::Orientation orientation, int role ) const{if(role==Qt::DisplayRole && orientation == Qt::Horizontal)return header[section];return QAbstractTableModel::headerData(section,orientation,role);}void WeaponModel::populateModel(){header<<tr("军种")<<tr("种类")<<tr("武器");army<<1<<2<<3<<4<<2<<4<<3<<1;weapon<<1<<3<<5<<7<<4<<8<<6<<2;type<<tr("b-2")<<tr("尼米兹级")<<tr("阿帕奇")<<tr("黄蜂级")<<tr("阿利伯克级")<<tr("AAAV")<<tr("M1A1")<<tr("F-22");}
main.cpp文件

//#include "mvcexample.h"#include <QtGui/QApplication>#include <QTextCodec>#include <QTableView>#include "weaponmodel.h"int main(int argc, char *argv[]){QApplication a(argc, argv);QTextCodec::setCodecForTr(QTextCodec::codecForLocale());WeaponModel model;QTableView view;view.setModel(&model);view.setWindowTitle(QObject::tr("自定义模型"));view.resize(640,480);view.show();//MVCExample w;//w.show();return a.exec();}

如图:摘自精通Qt4编程

原创粉丝点击