From 36d8dcc3bf9b4d31d86d2b739196dd52ebf226f2 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 27 Apr 2019 16:45:50 +0200 Subject: 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 --- qt-models/tankinfomodel.cpp | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) (limited to 'qt-models') 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(); } -- cgit v1.2.3-70-g09d2