自定义模型CurrencyModel

本文介绍了一个基于Qt的桌面应用程序示例,通过实现QAbstractTableModel来展示不同货币间的汇率转换。此示例使用QMap存储货币名称及其对应汇率,并通过CurrencyModel类展示这些数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  参考<<C++ GUI Programming with Qt 4>>中文版第二版中的例子:货币汇率。具体描述请看书本,主要重写继承自QAbstractTableModel的CurrencyModel,该模型底层的数据使用一个QMap<QString, double>类型的数据,其中key的QString是货币名字,value的double是这种货币对美元的汇率。

  currencymodel.h文件:

#ifndef CURRENCYMODEL_H
#define CURRENCYMODEL_H

#include <QAbstractTableModel>
#include <QMap>

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;
};

#endif

  currencymodel.cpp文件:

#include "currencymodel.h"

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();
}

  main.cpp文件:

#include <QtGui/QApplication>
#include "widget.h"
#include "currencymodel.h"
#include <QTableView>
#include <QMap>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QMap<QString, double> currencyMap;
    currencyMap.insert("AUD", 1.3259); //相对于美元的汇率
    currencyMap.insert("CHF", 1.2970);
    currencyMap.insert("SGD", 1.6901);
    currencyMap.insert("USD", 1.0000);

    CurrencyModel currencyModel;
    currencyModel.setCurrencyMap(currencyMap);

    QTableView tableView;
    tableView.setModel(&currencyModel);
    tableView.setAlternatingRowColors(true);
    tableView.setWindowTitle("Currencies");
    tableView.show();

    return a.exec();
}

  运行界面:

                                

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值