QT::QTableView的使用
来源:互联网 发布:淘宝无营业执照 编辑:程序博客网 时间:2024/05/20 20:21
QTableView采用的是类似MVC的内容与表现分离的设计模式
使用方法
[code lang="c"]
ModelEx model;
tableView->setModel(&model);
[/code]
[code lang="c"]
#ifndef MODELEX_H
#define MODELEX_H
#include <QAbstractTableModel>
#include <QVector>
#include <QMap>
#include <QStringList>
class ModelEx : public QAbstractTableModel
{
Q_OBJECT
public:
explicit ModelEx(QObject *parent = 0);
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const;
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const;
virtual QVariant data(const QModelIndex &index, int role) const;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
signals:
public slots:
private:
QVector<short> army;
QVector<short> weaponType;
QMap<short,QString> armyMap;
QMap<short,QString> weaponTypeMap;
QStringList weapon;
QStringList header;
void populateModel();
};
#endif // MODELEX_H
[code lang="c"]
#include "modelex.h"
ModelEx::ModelEx(QObject *parent) :
QAbstractTableModel(parent)
{
armyMap[1]=tr("空军");
armyMap[2]=tr("海军");
armyMap[3]=tr("陆军");
armyMap[4]=tr("海军陆战队");
weaponTypeMap[1]=tr("轰炸机");
weaponTypeMap[2]=tr("战斗机");
weaponTypeMap[3]=tr("航空母舰");
weaponTypeMap[4]=tr("驱逐舰");
weaponTypeMap[5]=tr("直升机");
weaponTypeMap[6]=tr("坦克");
header<<tr("军种")<<tr("种类")<<tr("武器");
army<<1<<2<<3<<4<<2<<4<<3<<1;
weaponType<<1<<3<<5<<7<<4<<8<<6<<2;
weapon<<tr("B-2")<<tr("尼米兹级")<<
("阿帕奇")<<tr("黄蜂级")<<
tr("阿利伯克级")<<tr("AAAV")<<
tr("M1A1")<<tr("F-22");
}
int ModelEx::columnCount(const QModelIndex &parent) const
{
return 3;
}
int ModelEx::rowCount(const QModelIndex &parent) const
{
return army.size();
}
QVariant ModelEx::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
if(role==Qt::DisplayRole)
{
switch(index.column())
{
case 0:
return armyMap[army[index.row()]];
break;
case 1:
return weaponTypeMap[weaponType[index.row()]];
break;
case 2:
return weapon[index.row()];
default:
return QVariant();
}
}
return QVariant();
}
QVariant ModelEx::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);
}
[/code]
查看原文:http://blog.wxameng.com/qt-qtableviewsample/
- qt的QTableView使用
- QT::QTableView的使用
- qt中qtableview的使用
- QT中的QTableView的使用
- Qt之QTableView的使用
- Qt中QTableView的使用
- Qt QTableview使用
- Qt QTableview使用
- 【转】Qt QTableview使用
- Qt QTableview使用
- Qt qtableview 使用
- Qt中Qtableview 的使用2(QStandardItemModel模型)
- Qt中Qtableview的使用1(QSqlTableModel模型)
- Qt QTableview的用法详解
- Qt Qtableview 的一些用法
- Qt QTableview的用法详解
- Qt Qtableview 的一些用法
- QT中Qtableview的使用方法
- GDOI改题小结
- hdu5303 环形路上取苹果的最小路程
- addr2line for kernel debug
- linux中添加tomcat,mysql,apache自动启动
- 数据结构(三):非线性逻辑结构-特殊的二叉树结构:堆、哈夫曼树、二叉搜索树、平衡二叉搜索树、红黑树、线索二叉树
- QT::QTableView的使用
- 程序员,你幸福吗?
- Kali Linux 卸载gnome桌面环境
- Java设计模式(二) 之 迭代器模式
- Poj1258 Agri-Net (Prim)
- 用svg实现上传图片进度条效果
- windows 多线程
- list set map 区别
- org.springframework.context.ApplicationContextAware使用理解