Qt学习之路(45): 自定义model之一

class CurrencyModel : public QAbstractTableModel
{
public:
CurrencyModel(QObject *parent = 0);
void setCurrencyMap(const QMap<QString, double> &map);
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
private:
QString currencyAt(int offset) const;
QMap<QString, double> currencyMap;
};
CurrencyModel::CurrencyModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
int CurrencyModel::rowCount(const QModelIndex & parent) const
{
return currencyMap.count();
}
int CurrencyModel::columnCount(const QModelIndex & parent) const
{
return currencyMap.count();
}
QVariant CurrencyModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::TextAlignmentRole) {
return int(Qt::AlignRight | Qt::AlignVCenter);
} else if (role == Qt::DisplayRole) {
QString rowCurrency = currencyAt(index.row());
QString columnCurrency = currencyAt(index.column());
if (currencyMap.value(rowCurrency) == 0.0)
return "####";
double amount = currencyMap.value(columnCurrency) / currencyMap.value(rowCurrency);
return QString("%1").arg(amount, 0, 'f', 4);
}
return QVariant();
}
QVariant CurrencyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
return currencyAt(section);
}
void CurrencyModel::setCurrencyMap(const QMap<QString, double> &map)
{
currencyMap = map;
reset();
}
QString CurrencyModel::currencyAt(int offset) const
{
return (currencyMap.begin() + offset).key();
}
QVariant CurrencyModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::TextAlignmentRole) {
return int(Qt::AlignRight | Qt::AlignVCenter);
} else if (role == Qt::DisplayRole) {
QString rowCurrency = currencyAt(index.row());
QString columnCurrency = currencyAt(index.column());
if (currencyMap.value(rowCurrency) == 0.0)
return "####";
double amount = currencyMap.value(columnCurrency) / currencyMap.value(rowCurrency);
return QString("%1").arg(amount, 0, 'f', 4);
}
return QVariant();
}
CurrencyTable::CurrencyTable()
{
QMap<QString, double> data;
data["NOK"] = 1.0000;
data["NZD"] = 0.2254;
data["SEK"] = 1.1991;
data["SGD"] = 0.2592;
data["USD"] = 0.1534;
CurrencyModel *model = new CurrencyModel;
model->setCurrencyMap(data);
QTableView *view = new QTableView(this);
view->setModel(model);
view->resize(400, 300);
}
Qt学习之路(45): 自定义model之一的更多相关文章
- Qt学习之路:自定义Model三篇,自定义委托等等
http://devbean.blog.51cto.com/448512/d-8/p-2
- Qt 学习之路:自定义事件
尽管 Qt 已经提供了很多事件,但对于更加千变万化的需求来说,有限的事件都是不够的.例如,我要支持一种新的设备,这个设备提供一种崭新的交互方式,那么,这种事件如何处理呢?所以,允许创建自己的事件 类型 ...
- Qt学习之路(54): 自定义拖放数据对象
前面的例子都是使用的系统提供的拖放对象 QMimeData 进行拖放数据的存储,比如使用 QMimeData::setText() 创建文本,使用 QMimeData::urls() 创建 URL 对 ...
- Qt 学习之路 2(53):自定义拖放数据
Qt 学习之路 2(53):自定义拖放数据 豆子 2013年5月26日 Qt 学习之路 2 13条评论上一章中,我们的例子使用系统提供的拖放对象QMimeData进行拖放数据的存储.比如使用QM ...
- Qt 学习之路 2(50):自定义可编辑模型
Home / Qt 学习之路 2 / Qt 学习之路 2(50):自定义可编辑模型 Qt 学习之路 2(50):自定义可编辑模型 豆子 2013年5月13日 Qt 学习之路 2 13条评论 上一章我们 ...
- Qt 学习之路 2(49):自定义只读模型
Qt 学习之路 2(49):自定义只读模型 豆子 2013年5月5日 Qt 学习之路 2 18条评论 model/view 模型将数据与视图分割开来,也就是说,我们可以为不同的视图,QListView ...
- Qt 学习之路 2(45):模型
Home / Qt 学习之路 2 / Qt 学习之路 2(45):模型 Qt 学习之路 2(45):模型 豆子 2013年2月26日 Qt 学习之路 2 23条评论 在前面两章的基础之上,我们 ...
- Qt 学习之路 2(41):model/view 架构
Qt 学习之路 2(41):model/view 架构 豆子 2013年1月23日 Qt 学习之路 2 50条评论 有时,我们的系统需要显示大量数据,比如从数据库中读取数据,以自己的方式显示在自己的应 ...
- Qt 学习之路 2(5):自定义信号槽
Home / Qt 学习之路 2 / Qt 学习之路 2(5):自定义信号槽 Qt 学习之路 2(5):自定义信号槽 豆子 2012年8月24日 Qt 学习之路 2 131条评论 上一节我们详 ...
随机推荐
- 构建一个基于UIView的类别
很多时候,如果我们想给我们的控件赋值,例如给控件的长度.宽度等赋值,很麻烦 需要先获取到当前frame,再整个临时frame来保存,修改赋值后再还给当前的frame,这都是重复性高的苦力活,解决方法就 ...
- Spark2 broadcast广播变量
A broadcast variable. Broadcast variables allow the programmer to keep a read-only variable cached o ...
- SharePoint 2013 字段属性之JSLink
在SharePoint 2013中,SPField新增加了一个属性是JSLink,使用客户端脚本修改字段前台展示,我们可以用很多方法修改这个脚本的引用,然后来修改脚本,下面,我们举一个简单的例子. 具 ...
- tomcat使用中的积累
一.清理tomcat缓存 项目运行出错,如:找不到某个类或方法,可能是没有部署好,而不是项目本身的问题.这个时候要重新部署.tomcat有缓存,所以有时候需要清理tomcat缓存再重新部署. 常用的几 ...
- 11.28JavaScript学习
JavaScript输出JavaScript通常用于操作HTML元素,如果要访问某个HTML元素,使用document.getElementById(id)方法,使用id属性标识HTML元素 文档输出 ...
- Ubuntu:/etc/rc.local 可执行权限问题
VmWare 11.1.2 安装Ubuntu 12.04系列版本, /etc/rc.local 有可执行权限. 安装Ubuntu 14.04系列版本,发现 /etc/rc.local 没有可执行权限, ...
- Crontab '2>&1 &' 含义
Crontab '2>&1 &' 含义 - dosttyy - 博客园 https://www.cnblogs.com/dosttyy/p/4810026.html
- 学习计划 nginx 中 php的配置详解
本章只看一个刚下载的nginx是如何支持php的 -- location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_inde ...
- LoadRunner-创建场景
录制完脚本,并调试运行正常后,想要模拟并发进行压力测试,需要创建场景. 1.点击Tools->Create Controller Scenario... 2.选择手工场景,并设置并发用户数.点击 ...
- TortoiseGit的使用
TortoiseGit只是一个外壳而已,它调用的是msysgit,相当于msysgit的windows gui而已,如果喜欢用git命令行,那就不需要安装它. 所以要先安装msysgit(window ...