Qt MVC之自定义模型
来源:互联网 发布:suse linux 安装教程 编辑:程序博客网 时间:2024/05/18 13:27
Qt中所有模型都是基于QAbstractItemModel派生的。当数据改变时,模型发出信号通知视图。为了保证数据的存取和表示分离,InterView
引入了模型索引(Model Index)的概念。
通过模型索引来存取数据条目,必须有三个属性:行号,列号和父条目的模型索引。下面三种常见的模型索引组织方式:
1.表模型:行和列
2.列表模型:类似于表模型
3.树模型:行,列和父索引。
如果要实现自己的模型,可以从通用的QAbstractItemModel类继承,也可以从类QAbstractListModel和QAbstractTableModel继承实现列表
如图:摘自精通Qt4编程
引入了模型索引(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_Hweaponmodel.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编程
- Qt MVC之自定义模型
- Qt之模型/视图(自定义风格)
- Qt之模型/视图(自定义风格)
- Qt之模型/视图(自定义进度条)
- Qt之模型/视图(自定义按钮)
- Qt之模型/视图(自定义按钮)
- Qt之模型/视图(自定义风格)
- Qt之模型/视图(自定义风格)
- QT MVC 模型/视图
- QT MVC模型
- Qt学习:项视图类之自定义模型
- Qt 学习之路 2(49):自定义只读模型
- Qt之MVC使用
- QT之QSqlTableModel模型
- Qt之模型/视图
- Qt之模型/视图
- Qt之模型/视图
- Qt之模型/视图
- “网站加载速度”与谷歌搜索排名有何联系
- 在某一时刻之后让提交按钮失去效果 并且将按钮置灰
- AS3.0实现淡入淡出效果
- Java中getClassLoader().getResourceAsStream("XXX")的用法
- poj 1006 生理周期 中国剩余定理
- Qt MVC之自定义模型
- Linux下c开发 之 线程通信
- Android 利用反射实现不安装直接运行APK(动态加载)
- sgu——261(数论之N次剩余问题)
- 窗口重绘一点通
- 子窗口与父窗口的消息传递
- opencv识别正方形(矩形)代码(转)
- Reactor模式 - @ libevent by c【转载内容】
- VC 绘图,使用双缓冲技术实现