currencymodel.cpp

来自「c++ GUI Programming with QT4书中的源码」· C++ 代码 · 共 61 行

CPP
61
字号
#include <QtCore>#include "currencymodel.h"CurrencyModel::CurrencyModel(QObject *parent)    : QAbstractTableModel(parent){}void CurrencyModel::setCurrencyMap(const QMap<QString, double> &map){    currencyMap = map;    reset();}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);}QString CurrencyModel::currencyAt(int offset) const{    return (currencyMap.begin() + offset).key();}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?