自定义只读模型
来源:互联网 发布:ubuntu安装pyqt 编辑:程序博客网 时间:2024/06/08 07:13
,cpp文件源码
#include "currencymodel.h"
CurrencyModel::CurrencyModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
CurrencyModel::~CurrencyModel()
{
}
int CurrencyModel::rowCount(const QModelIndex &parent) const
{
return currencyMap.count();
}
int CurrencyModel::columnCount(const QModelIndex & parent)const
{
return currencyMap.count();
}
QVariant CurrencyModel::headerData( int section,Qt::Orientation,int role) const
{
if (role !=Qt::DisplayRole)
{
return QVariant();
}
return currencyAt(section);
}
QString CurrencyModel::currencyAt(int offset) const
{
return(currencyMap.begin()+offset).key();
}
void CurrencyModel::setCurrencyMap(const QMap<QString,double> &map)
{
beginResetModel(); // 写一种协议
currencyMap = map;
endResetModel();
}
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();
}
.h 文件源码
#ifndef CURRENCYMODEL_H
#define CURRENCYMODEL_H
#include <QAbstractTableModel>
class CurrencyModel : public QAbstractTableModel
{
Q_OBJECT
public:
CurrencyModel(QObject *parent=0);
~CurrencyModel();
void setCurrencyMap(const QMap<QString, double> &map); // 非const的成员函数不可以被const的对象访问
int rowCount(const QModelIndex &parent) const; // this指针常量化 任何修改this成员变量的操作都是不允许的 即可被const对象访问也可以被非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;
};
#endif // CURRENCYMODEL_H
mian 函数的实现代码 :
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMap<QString, double> data;
data["USD"] = 1.0000;
data["CNY"] = 0.1628;
data["GBP"] = 1.5361;
data["EUR"] = 1.2992;
data["HKD"] = 0.1289;
QTableView view;
CurrencyModel *model = new CurrencyModel(&view);
model->setCurrencyMap(data);
view.setModel(model);
view.resize(400, 300);
view.show();
return app.exec();
}
实现效果:
- 自定义只读模型
- Qt 学习之路 2(49):自定义只读模型
- 自定义UICollectionViewController只读UICollectionViewLayout
- Qt 学习之路 2(49):自定义只读模型 QTableView
- xaf实现自定义只读参数
- 自定义模型
- 如何设置JavaScript某个自定义的对象属性为只读
- 实现自定义模型
- 自定义模型使用教程
- ThinkPHP 引入自定义模型
- 自定义英雄模型导入
- 采集 自定义模型
- dede 自定义模型
- (三十九)自定义模型
- <Shader> 自定义光照模型
- 自定义模型类
- 读取自定义模型文件,绘制模型
- 深入理解模型视图、自定义模型
- CAIOJ1034 二叉树的后序遍历(经典递归)
- 【spring配置】——spring整合Quartz定时器
- Xcode 真机运行报错:the application could not be verified.
- VMware虚拟化
- [2017纪中11-1]背包 二分
- 自定义只读模型
- 服务器开发整合
- python将文件夹里所有的txt文件内容写入excel
- vmware 14 pro许可证秘钥
- AWS security group允许同组内主机访问
- 数据库优化之MySQL优化(三):
- ExtJS 4.2 treeStore设置autoLoad但是还是会自动加载的问题
- Git 【Mac 系统下搭建本地 Git 服务器】
- Android 获取inflate(打气筒)的方式