diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-04-27 16:45:50 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-04-29 13:06:39 -0700 |
commit | 36d8dcc3bf9b4d31d86d2b739196dd52ebf226f2 (patch) | |
tree | 0a39483191bbb2e30092f11ee6d8bb8aa5b87b61 | |
parent | 4f0fd86d3530cf921e63e8a462946a8dfccff5ae (diff) | |
download | subsurface-36d8dcc3bf9b4d31d86d2b739196dd52ebf226f2.tar.gz |
Cleanup: implement proper Qt-model semantics in TankInfoModel
- Use a beginResetModel()/endResetModel() pair instead of distinct
addRows / removeRows pairs.
- Reuse the update function in the constructor().
- Let "rows" be the number of rows, not the number of rows minus one.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | qt-models/tankinfomodel.cpp | 29 |
1 files changed, 7 insertions, 22 deletions
diff --git a/qt-models/tankinfomodel.cpp b/qt-models/tankinfomodel.cpp index e91aa4d69..df1841fa3 100644 --- a/qt-models/tankinfomodel.cpp +++ b/qt-models/tankinfomodel.cpp @@ -79,35 +79,20 @@ QVariant TankInfoModel::data(const QModelIndex &index, int role) const int TankInfoModel::rowCount(const QModelIndex&) const { - return rows + 1; + return rows; } -TankInfoModel::TankInfoModel() : rows(-1) +TankInfoModel::TankInfoModel() { setHeaderDataStrings(QStringList() << tr("Description") << tr("ml") << tr("bar")); - struct tank_info_t *info = tank_info; - for (info = tank_info; info->name && info < tank_info + MAX_TANK_INFO; info++, rows++) - ; - - if (rows > -1) { - beginInsertRows(QModelIndex(), 0, rows); - endInsertRows(); - } + update(); } void TankInfoModel::update() { - if (rows > -1) { - beginRemoveRows(QModelIndex(), 0, rows); - endRemoveRows(); - rows = -1; - } - struct tank_info_t *info = tank_info; - for (info = tank_info; info->name && info < tank_info + MAX_TANK_INFO; info++, rows++) + beginResetModel(); + rows = 0; + for (struct tank_info_t *info = tank_info; info->name && info < tank_info + MAX_TANK_INFO; info++, rows++) ; - - if (rows > -1) { - beginInsertRows(QModelIndex(), 0, rows); - endInsertRows(); - } + endResetModel(); } |