zoukankan      html  css  js  c++  java
  • 自定义模型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();
    }

      运行界面:

                                    

  • 相关阅读:
    python中特殊参数self的作用
    python中类的初始化案例
    python中类的调用
    Python--网络编程-----struct模块的字节数限制
    Python--网络编程-----解决粘包问题-简单版
    Python--网络编程-----粘包的底层原理分析
    Python--网络编程-----粘包现象
    Python--网络编程-----socket编程示例--模拟ssh远程执行命令
    pycharm下 os.system执行命令返回有中文乱码
    Python--网络编程-----socket编程示例--打电话--加上链接循环
  • 原文地址:https://www.cnblogs.com/venow/p/2731895.html
Copyright © 2011-2022 走看看