自定义模型CurrencyModel

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

  运行界面:

                                

原文地址:https://www.cnblogs.com/venow/p/2731895.html