From d4ff1ffb614568f596f24cfe7cb3d03c675c34d0 Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Mon, 2 Feb 2026 16:48:42 +0100 Subject: [PATCH] Split TableModel::updateItemsFromJson(...) into two functions and added support for multiple items in JSON data. --- genericcore.cpp | 2 -- model/tablemodel.cpp | 27 +++++++++++++++------------ model/tablemodel.h | 1 + 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/genericcore.cpp b/genericcore.cpp index 1327f67..44875c5 100644 --- a/genericcore.cpp +++ b/genericcore.cpp @@ -158,9 +158,7 @@ void GenericCore::onItemsFetchFailure(const QString errorString) { } void GenericCore::onPostRequestSuccessful(const QByteArray responseData) { - // NEXT search local item and set server generated UUID; const QString message = m_mainModel->updateItemsFromJson(responseData); - emit displayStatusMessage(message); } diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index c78bd70..76e87a7 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -182,27 +182,30 @@ QByteArray TableModel::jsonDataForServer(const QModelIndex& currentIndex) const } QString TableModel::updateItemsFromJson(const QByteArray& jsonData) { - /// convert JSON data into a list of item values const QList valueList = JsonParser::toItemValuesList(jsonData, ITEM_KEY_STRING); - /// for each item values: - // NEXT iterate over all value items in the list (or disable updating multiple items for now) - // for (ModelItemValues itemValues : valueList) { - // } - // NEXT encapsulate into updateItem(const ModelItemValues& itemValues) - QModelIndex foundIndex = searchItemIndex(valueList.first()); + int nChangedItems = 0; + for (const ModelItemValues& itemValues : valueList) { + bool updateHappened = updateItem(itemValues); + if (updateHappened) { + nChangedItems++; + } + } + return QString("Found and updated %1 item(s).").arg(nChangedItems); +} + +bool TableModel::updateItem(const ModelItemValues& itemValues) { + QModelIndex foundIndex = searchItemIndex(itemValues); qDebug() << "Search done!"; if (foundIndex == QModelIndex()) { const QString errorMessage = "No matching item found!"; qWarning() << errorMessage; - return errorMessage; + return false; } else { qInfo() << "Item found!"; /// update existing item - QMap roles = valueList.first(); - setItemData(foundIndex, roles); - /// return status what happened in this function (i. e. "Created x items, updated y items.") - return QString("Item found at row %1.").arg(foundIndex.row()); + setItemData(foundIndex, itemValues); + return true; } } diff --git a/model/tablemodel.h b/model/tablemodel.h index 84c592f..a7d2089 100644 --- a/model/tablemodel.h +++ b/model/tablemodel.h @@ -41,6 +41,7 @@ class TableModel : public QAbstractTableModel { QByteArray jsonDataForServer(const QModelIndex& currentIndex) const; QString updateItemsFromJson(const QByteArray& jsonData); + bool updateItem(const ModelItemValues& itemValues); public slots: // bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())