From a50c9a450d7307140dbab2eae875939baee370b7 Mon Sep 17 00:00:00 2001 From: leichaojian Date: Fri, 21 Nov 2014 20:04:30 +0800 Subject: [PATCH] add currencymodel --- CurrencyModel/CurrencyModel.pro | 20 +++ CurrencyModel/CurrencyModel.pro.user | 254 +++++++++++++++++++++++++++ CurrencyModel/currencymodel.cpp | 70 ++++++++ CurrencyModel/currencymodel.h | 34 ++++ CurrencyModel/currencymodel.ui | 20 +++ CurrencyModel/main.cpp | 35 ++++ 6 files changed, 433 insertions(+) create mode 100644 CurrencyModel/CurrencyModel.pro create mode 100644 CurrencyModel/CurrencyModel.pro.user create mode 100644 CurrencyModel/currencymodel.cpp create mode 100644 CurrencyModel/currencymodel.h create mode 100644 CurrencyModel/currencymodel.ui create mode 100644 CurrencyModel/main.cpp diff --git a/CurrencyModel/CurrencyModel.pro b/CurrencyModel/CurrencyModel.pro new file mode 100644 index 0000000..35cedcf --- /dev/null +++ b/CurrencyModel/CurrencyModel.pro @@ -0,0 +1,20 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2014-11-21T19:28:10 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = CurrencyModel +TEMPLATE = app + + +SOURCES += main.cpp\ + currencymodel.cpp + +HEADERS += currencymodel.h + +FORMS += currencymodel.ui diff --git a/CurrencyModel/CurrencyModel.pro.user b/CurrencyModel/CurrencyModel.pro.user new file mode 100644 index 0000000..0362370 --- /dev/null +++ b/CurrencyModel/CurrencyModel.pro.user @@ -0,0 +1,254 @@ + + + + + + EnvironmentId + {a8c18b7e-8020-49b9-a0ef-0165c784344f} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + + ProjectExplorer.Project.Target.0 + + Desktop Qt 5.3 MinGW 32bit + Desktop Qt 5.3 MinGW 32bit + qt.53.win32_mingw482_kit + 0 + 0 + 0 + + C:/Users/fzyz_sb/Desktop/build-CurrencyModel-Desktop_Qt_5_3_MinGW_32bit-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + true + + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + 构建 + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + 清理 + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + C:/Users/fzyz_sb/Desktop/build-CurrencyModel-Desktop_Qt_5_3_MinGW_32bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + true + + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + 构建 + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + 清理 + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 2 + + + 0 + 部署 + + ProjectExplorer.BuildSteps.Deploy + + 1 + 在本地部署 + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + -1 + + + + false + %{buildDir} + 自定义执行档 + + ProjectExplorer.CustomExecutableRunConfiguration + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 16 + + + Version + 16 + + diff --git a/CurrencyModel/currencymodel.cpp b/CurrencyModel/currencymodel.cpp new file mode 100644 index 0000000..b1637f7 --- /dev/null +++ b/CurrencyModel/currencymodel.cpp @@ -0,0 +1,70 @@ +#include "currencymodel.h" +#include "ui_currencymodel.h" + +CurrencyModel::CurrencyModel(QWidget *parent) : + QAbstractTableModel(parent), + ui(new Ui::CurrencyModel) +{ +} + +CurrencyModel::~CurrencyModel() +{ + delete ui; +} + +void CurrencyModel::setCurrencyMap(const QMap &map) +{ + currencyMap = map; +} + +int CurrencyModel::rowCount(const QModelIndex &parent) const +{ + return currencyMap.count(); +} +int CurrencyModel::columnCount(const QModelIndex &parent) const +{ + return currencyMap.count(); +} + +//role为角色,不同的角色执行不同的动作,通过索引index来进行数据的读取 +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()); + + //0不能被除 + 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(); +} diff --git a/CurrencyModel/currencymodel.h b/CurrencyModel/currencymodel.h new file mode 100644 index 0000000..8934757 --- /dev/null +++ b/CurrencyModel/currencymodel.h @@ -0,0 +1,34 @@ +#ifndef CURRENCYMODEL_H +#define CURRENCYMODEL_H + +#include +#include + +namespace Ui { +class CurrencyModel; +} + +class CurrencyModel : public QAbstractTableModel +{ + Q_OBJECT + +public: + explicit CurrencyModel(QWidget *parent = 0); + ~CurrencyModel(); + + void setCurrencyMap(const QMap &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 currencyMap; + +private: + Ui::CurrencyModel *ui; +}; + +#endif // CURRENCYMODEL_H diff --git a/CurrencyModel/currencymodel.ui b/CurrencyModel/currencymodel.ui new file mode 100644 index 0000000..b9affd3 --- /dev/null +++ b/CurrencyModel/currencymodel.ui @@ -0,0 +1,20 @@ + + CurrencyModel + + + + 0 + 0 + 400 + 300 + + + + CurrencyModel + + + + + + + diff --git a/CurrencyModel/main.cpp b/CurrencyModel/main.cpp new file mode 100644 index 0000000..40a178c --- /dev/null +++ b/CurrencyModel/main.cpp @@ -0,0 +1,35 @@ +#include "currencymodel.h" +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + QMap currencyMap; + currencyMap.insert("AUD", 1.3259); + currencyMap.insert("CHF", 1.2970); + currencyMap.insert("CZK", 24.510); + currencyMap.insert("DKK", 6.2168); + currencyMap.insert("EUR", 0.8333); + currencyMap.insert("GBP", 0.5661); + currencyMap.insert("HKD", 7.7562); + currencyMap.insert("JPY", 112.92); + currencyMap.insert("NOK", 6.5200); + currencyMap.insert("NZD", 1.4697); + currencyMap.insert("SEK", 7.8180); + currencyMap.insert("SGD", 1.6901); + currencyMap.insert("USD", 1.0000); + + CurrencyModel w; + w.setCurrencyMap(currencyMap); + + QTableView tableView; + tableView.setModel(&w); + tableView.setAlternatingRowColors(true); + + tableView.setWindowTitle(QObject::tr("Currencies")); + tableView.show(); + + return a.exec(); +}