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条评论 上一节我们详 ...
 
随机推荐
- 在 arc里面打印 引用计数的方法
			
查阅资料: You can use CFGetRetainCount with Objective-C objects, even under ARC: NSLog(@"Retain c ...
 - vue--提取公共方法
			
在做一个项目的时候,一些组件内公用的方法可以单独提取出来做复用: 参考:https://www.jb51.net/article/115662.htm 简单示例: 代码: const config = ...
 - 2018牛客网暑期ACM多校训练营(第一场) J - Different Integers - [莫队算法]
			
题目链接:https://www.nowcoder.com/acm/contest/139/J 题目描述 Given a sequence of integers a1, a2, ..., an a ...
 - 设置自己的APP能打开文件(在其他应用中打开显示自己的应用)
			
http://blog.csdn.net/leewolf130/article/details/29568961 http://www.jianshu.com/p/9711c3daf4bb https ...
 - python3学习笔记(4)_function-参数
			
#python学习笔记 17/07/10 # !/usr/bin/evn python3 # -*- coding:utf-8 -*- import math #函数 函数的 定义 #定义一个求绝对值 ...
 - 设置elasticsearch一次最大数量查询
			
PUT my_index/_settings?preserve_existing=true{ "max_result_window": "2000000000" ...
 - 洛谷P4931 情侣!给我!烧了! 数论
			
正解:数论 解题报告: 传送门 这题,想不到就很痛苦,但是理解了之后还是觉得也没有很难,,,毕竟实现不难QAQ 首先关于前面k对情侣的很简单,就是C(n,k)*C(n,k)*A(k,k)*2k 随便解 ...
 - Shell的>/dev/null、2>&1、2>1
			
转载自:http://dos2unix.cn/link/480 1. 标准输入stdin文件描述符为0,标准输出stdout文件描述符为1,标准错误stderr文件描述符为2 2. /dev/null ...
 - FineReport实现java报表权限使用的效果图
			
Java报表-多级权限配置说明 Java报表-联合填报 Java报表-模板内容权限控制 Java报表-权限细粒度控制
 - 查看修改MySQL字符集
			
查看修改MySQL字符集 http://blog.sina.com.cn/s/blog_70ac6bec01016fts.html 查看修改MySQL字符集 (2012-08-22 09:53:21) ...